bitkeeper revision 1.166 (3e9c248dVGQSFqCQP51wgrGBpJNZoA)
authorsmh22@boulderdash.cl.cam.ac.uk <smh22@boulderdash.cl.cam.ac.uk>
Tue, 15 Apr 2003 15:26:05 +0000 (15:26 +0000)
committersmh22@boulderdash.cl.cam.ac.uk <smh22@boulderdash.cl.cam.ac.uk>
Tue, 15 Apr 2003 15:26:05 +0000 (15:26 +0000)
initial support for ide cdrom drives

.rootkeys
xen/Rules.mk
xen/drivers/Makefile
xen/drivers/block/ll_rw_blk.c
xen/drivers/cdrom/Makefile [new file with mode: 0644]
xen/drivers/cdrom/cdrom.c [new file with mode: 0644]
xen/drivers/ide/ide-cd.c [new file with mode: 0644]
xen/drivers/ide/ide-cd.h [new file with mode: 0644]
xen/include/xeno/cdrom.h [new file with mode: 0644]
xenolinux-2.4.21-pre4-sparse/arch/xeno/defconfig

index 746275c4d2352553cb41573b1088ebcf34bae230..41c75073815907d92a47f1af1058b239a2c88e77 100644 (file)
--- a/.rootkeys
+++ b/.rootkeys
 3ddb79beyWwLRP_BiM2t1JKgr_plEw xen/drivers/block/ll_rw_blk.c
 3e4a8cb7RhubVgsPwO7cK0pgAN8WCQ xen/drivers/block/xen_block.c
 3e5d129asHNyZOjBKTkqs-9AFzxemA xen/drivers/block/xen_segment.c
+3e9c248afxxsnAzIt2na7Ej24yNFzg xen/drivers/cdrom/Makefile
+3e9c248ajUkn2W3n4vgm72Hp2ftZ8A xen/drivers/cdrom/cdrom.c
 3e4a8cb7alzQCDKS7MlioPoHBKYkdQ xen/drivers/char/Makefile
 3e4a8cb7WmiYdC-ASGiCSG_CL8vsqg xen/drivers/char/xen_kbd.c
 3e4a8cb7nMChlro4wvOBo76n__iCFA xen/drivers/char/xen_serial.c
 3ddb79bdhcqD9ebrslr0O0oHqTiiXg xen/drivers/ide/Makefile
+3e9c248aCM6Lex1Am8_NJIeesN4kKg xen/drivers/ide/ide-cd.c
+3e9c248aFfSNR_hl-WQBbv-R9CTgzg xen/drivers/ide/ide-cd.h
 3ddb79bdErDn_WC3G-fWxKNR3viLnA xen/drivers/ide/ide-disk.c
 3ddb79bdIPNW36FrlId94jTXaW8HoA xen/drivers/ide/ide-dma.c
 3ddb79be5Ysvhn4se_Z-LQY_hI6UPw xen/drivers/ide/ide-features.c
 3ddb79c116WbJV8bwGZXFFJy_GNNvw xen/include/xeno/byteorder/swab.h
 3ddb79c1pwmlw8VXW8aaSKAVGVmjDA xen/include/xeno/byteorder/swabb.h
 3ddb79c0c0cX_DZE209-Bb-Rx1v-Aw xen/include/xeno/cache.h
+3e9c248aEG_nCngztiFmv5CfayNkcA xen/include/xeno/cdrom.h
 3ddb79c259jh8hE7vre_8NuE7nwNSA xen/include/xeno/config.h
 3ddb79c1V44RD26YqCUm-kqIupM37A xen/include/xeno/ctype.h
 3ddb79c05DdHQ0UxX_jKsXdR4QlMCA xen/include/xeno/delay.h
index d0feffd7f947e31eaa6bbd6fb5cb1f11cb996aa0..4e15dde1bfa8c189242a1e305429f278398d5925 100644 (file)
@@ -19,6 +19,7 @@ ALL_OBJS += $(BASEDIR)/drivers/char/driver.o
 ALL_OBJS += $(BASEDIR)/drivers/pci/driver.o
 ALL_OBJS += $(BASEDIR)/drivers/net/driver.o
 ALL_OBJS += $(BASEDIR)/drivers/block/driver.o
+ALL_OBJS += $(BASEDIR)/drivers/cdrom/driver.o
 ALL_OBJS += $(BASEDIR)/drivers/ide/driver.o
 ALL_OBJS += $(BASEDIR)/drivers/scsi/driver.o
 ALL_OBJS += $(BASEDIR)/arch/$(ARCH)/arch.o
index 4aa76a3f25975eecba509913842787922cb8db15..814ed909d84b3b5a69a4b064bab86efd225d5030 100644 (file)
@@ -4,6 +4,7 @@ default:
        $(MAKE) -C pci
        $(MAKE) -C net
        $(MAKE) -C block
+       $(MAKE) -C cdrom
        $(MAKE) -C ide
        $(MAKE) -C scsi
 
@@ -12,5 +13,6 @@ clean:
        $(MAKE) -C pci clean
        $(MAKE) -C net clean
        $(MAKE) -C block clean
+       $(MAKE) -C cdrom clean
        $(MAKE) -C ide clean
        $(MAKE) -C scsi clean
index 870b5cdb85f3b93389bcc753b8bae12dc8a2c7cc..55fbdf3e7944d166f26a547bca174f4185c0f44e 100644 (file)
 #endif
 
 /* This will die as all synchronous stuff is coming to an end */
+#if 0 
 #define complete(_r) panic("completion.h stuff may be needed...")
+#else
+// XXX SMH: we spin when waiting for completion so just toggle flag 
+#define complete(_r) (*(int *)(_r) = 0)
+#endif
+
+
 
 /*
  * MAC Floppy IWM hooks
diff --git a/xen/drivers/cdrom/Makefile b/xen/drivers/cdrom/Makefile
new file mode 100644 (file)
index 0000000..729f949
--- /dev/null
@@ -0,0 +1,7 @@
+
+include $(BASEDIR)/Rules.mk
+
+default: $(OBJS)
+       $(LD) -r -o driver.o $(OBJS)
+clean:
+       rm -f *.o *~ core
diff --git a/xen/drivers/cdrom/cdrom.c b/xen/drivers/cdrom/cdrom.c
new file mode 100644 (file)
index 0000000..8bf93cc
--- /dev/null
@@ -0,0 +1,2665 @@
+/* linux/drivers/cdrom/cdrom.c. 
+   Copyright (c) 1996, 1997 David A. van Leeuwen.
+   Copyright (c) 1997, 1998 Erik Andersen <andersee@debian.org>
+   Copyright (c) 1998, 1999 Jens Axboe <axboe@image.dk>
+
+   May be copied or modified under the terms of the GNU General Public
+   License.  See linux/COPYING for more information.
+
+   Uniform CD-ROM driver for Linux.
+   See Documentation/cdrom/cdrom-standard.tex for usage information.
+
+   The routines in the file provide a uniform interface between the
+   software that uses CD-ROMs and the various low-level drivers that
+   actually talk to the hardware. Suggestions are welcome.
+   Patches that work are more welcome though.  ;-)
+
+ To Do List:
+ ----------------------------------
+
+ -- Modify sysctl/proc interface. I plan on having one directory per
+ drive, with entries for outputing general drive information, and sysctl
+ based tunable parameters such as whether the tray should auto-close for
+ that drive. Suggestions (or patches) for this welcome!
+
+
+ Revision History
+ ----------------------------------
+ 1.00  Date Unknown -- David van Leeuwen <david@tm.tno.nl>
+ -- Initial version by David A. van Leeuwen. I don't have a detailed
+  changelog for the 1.x series, David?
+
+2.00  Dec  2, 1997 -- Erik Andersen <andersee@debian.org>
+  -- New maintainer! As David A. van Leeuwen has been too busy to activly
+  maintain and improve this driver, I am now carrying on the torch. If
+  you have a problem with this driver, please feel free to contact me.
+
+  -- Added (rudimentary) sysctl interface. I realize this is really weak
+  right now, and is _very_ badly implemented. It will be improved...
+
+  -- Modified CDROM_DISC_STATUS so that it is now incorporated into
+  the Uniform CD-ROM driver via the cdrom_count_tracks function.
+  The cdrom_count_tracks function helps resolve some of the false
+  assumptions of the CDROM_DISC_STATUS ioctl, and is also used to check
+  for the correct media type when mounting or playing audio from a CD.
+
+  -- Remove the calls to verify_area and only use the copy_from_user and
+  copy_to_user stuff, since these calls now provide their own memory
+  checking with the 2.1.x kernels.
+
+  -- Major update to return codes so that errors from low-level drivers
+  are passed on through (thanks to Gerd Knorr for pointing out this
+  problem).
+
+  -- Made it so if a function isn't implemented in a low-level driver,
+  ENOSYS is now returned instead of EINVAL.
+
+  -- Simplified some complex logic so that the source code is easier to read.
+
+  -- Other stuff I probably forgot to mention (lots of changes).
+
+2.01 to 2.11 Dec 1997-Jan 1998
+  -- TO-DO!  Write changelogs for 2.01 to 2.12.
+
+2.12  Jan  24, 1998 -- Erik Andersen <andersee@debian.org>
+  -- Fixed a bug in the IOCTL_IN and IOCTL_OUT macros.  It turns out that
+  copy_*_user does not return EFAULT on error, but instead returns the number 
+  of bytes not copied.  I was returning whatever non-zero stuff came back from 
+  the copy_*_user functions directly, which would result in strange errors.
+
+2.13  July 17, 1998 -- Erik Andersen <andersee@debian.org>
+  -- Fixed a bug in CDROM_SELECT_SPEED where you couldn't lower the speed
+  of the drive.  Thanks to Tobias Ringstr|m <tori@prosolvia.se> for pointing
+  this out and providing a simple fix.
+  -- Fixed the procfs-unload-module bug with the fill_inode procfs callback.
+  thanks to Andrea Arcangeli
+  -- Fixed it so that the /proc entry now also shows up when cdrom is
+  compiled into the kernel.  Before it only worked when loaded as a module.
+
+  2.14 August 17, 1998 -- Erik Andersen <andersee@debian.org>
+  -- Fixed a bug in cdrom_media_changed and handling of reporting that
+  the media had changed for devices that _don't_ implement media_changed.  
+  Thanks to Grant R. Guenther <grant@torque.net> for spotting this bug.
+  -- Made a few things more pedanticly correct.
+
+2.50 Oct 19, 1998 - Jens Axboe <axboe@image.dk>
+  -- New maintainers! Erik was too busy to continue the work on the driver,
+  so now Chris Zwilling <chris@cloudnet.com> and Jens Axboe <axboe@image.dk>
+  will do their best to follow in his footsteps
+  
+  2.51 Dec 20, 1998 - Jens Axboe <axboe@image.dk>
+  -- Check if drive is capable of doing what we ask before blindly changing
+  cdi->options in various ioctl.
+  -- Added version to proc entry.
+  
+  2.52 Jan 16, 1999 - Jens Axboe <axboe@image.dk>
+  -- Fixed an error in open_for_data where we would sometimes not return
+  the correct error value. Thanks Huba Gaspar <huba@softcell.hu>.
+  -- Fixed module usage count - usage was based on /proc/sys/dev
+  instead of /proc/sys/dev/cdrom. This could lead to an oops when other
+  modules had entries in dev. Feb 02 - real bug was in sysctl.c where
+  dev would be removed even though it was used. cdrom.c just illuminated
+  that bug.
+  
+  2.53 Feb 22, 1999 - Jens Axboe <axboe@image.dk>
+  -- Fixup of several ioctl calls, in particular CDROM_SET_OPTIONS has
+  been "rewritten" because capabilities and options aren't in sync. They
+  should be...
+  -- Added CDROM_LOCKDOOR ioctl. Locks the door and keeps it that way.
+  -- Added CDROM_RESET ioctl.
+  -- Added CDROM_DEBUG ioctl. Enable debug messages on-the-fly.
+  -- Added CDROM_GET_CAPABILITY ioctl. This relieves userspace programs
+  from parsing /proc/sys/dev/cdrom/info.
+  
+  2.54 Mar 15, 1999 - Jens Axboe <axboe@image.dk>
+  -- Check capability mask from low level driver when counting tracks as
+  per suggestion from Corey J. Scotts <cstotts@blue.weeg.uiowa.edu>.
+  
+  2.55 Apr 25, 1999 - Jens Axboe <axboe@image.dk>
+  -- autoclose was mistakenly checked against CDC_OPEN_TRAY instead of
+  CDC_CLOSE_TRAY.
+  -- proc info didn't mask against capabilities mask.
+  
+  3.00 Aug 5, 1999 - Jens Axboe <axboe@image.dk>
+  -- Unified audio ioctl handling across CD-ROM drivers. A lot of the
+  code was duplicated before. Drives that support the generic packet
+  interface are now being fed packets from here instead.
+  -- First attempt at adding support for MMC2 commands - for DVD and
+  CD-R(W) drives. Only the DVD parts are in now - the interface used is
+  the same as for the audio ioctls.
+  -- ioctl cleanups. if a drive couldn't play audio, it didn't get
+  a change to perform device specific ioctls as well.
+  -- Defined CDROM_CAN(CDC_XXX) for checking the capabilities.
+  -- Put in sysctl files for autoclose, autoeject, check_media, debug,
+  and lock.
+  -- /proc/sys/dev/cdrom/info has been updated to also contain info about
+  CD-Rx and DVD capabilities.
+  -- Now default to checking media type.
+  -- CDROM_SEND_PACKET ioctl added. The infrastructure was in place for
+  doing this anyway, with the generic_packet addition.
+  
+  3.01 Aug 6, 1999 - Jens Axboe <axboe@image.dk>
+  -- Fix up the sysctl handling so that the option flags get set
+  correctly.
+  -- Fix up ioctl handling so the device specific ones actually get
+  called :).
+  
+  3.02 Aug 8, 1999 - Jens Axboe <axboe@image.dk>
+  -- Fixed volume control on SCSI drives (or others with longer audio
+  page).
+  -- Fixed a couple of DVD minors. Thanks to Andrew T. Veliath
+  <andrewtv@usa.net> for telling me and for having defined the various
+  DVD structures and ioctls in the first place! He designed the original
+  DVD patches for ide-cd and while I rearranged and unified them, the
+  interface is still the same.
+  
+  3.03 Sep 1, 1999 - Jens Axboe <axboe@image.dk>
+  -- Moved the rest of the audio ioctls from the CD-ROM drivers here. Only
+  CDROMREADTOCENTRY and CDROMREADTOCHDR are left.
+  -- Moved the CDROMREADxxx ioctls in here.
+  -- Defined the cdrom_get_last_written and cdrom_get_next_block as ioctls
+  and exported functions.
+  -- Erik Andersen <andersen@xmission.com> modified all SCMD_ commands
+  to now read GPCMD_ for the new generic packet interface. All low level
+  drivers are updated as well.
+  -- Various other cleanups.
+
+  3.04 Sep 12, 1999 - Jens Axboe <axboe@image.dk>
+  -- Fixed a couple of possible memory leaks (if an operation failed and
+  we didn't free the buffer before returning the error).
+  -- Integrated Uniform CD Changer handling from Richard Sharman
+  <rsharman@pobox.com>.
+  -- Defined CD_DVD and CD_CHANGER log levels.
+  -- Fixed the CDROMREADxxx ioctls.
+  -- CDROMPLAYTRKIND uses the GPCMD_PLAY_AUDIO_MSF command - too few
+  drives supported it. We loose the index part, however.
+  -- Small modifications to accomodate opens of /dev/hdc1, required
+  for ide-cd to handle multisession discs.
+  -- Export cdrom_mode_sense and cdrom_mode_select.
+  -- init_cdrom_command() for setting up a cgc command.
+  
+  3.05 Oct 24, 1999 - Jens Axboe <axboe@image.dk>
+  -- Changed the interface for CDROM_SEND_PACKET. Before it was virtually
+  impossible to send the drive data in a sensible way.
+  -- Lowered stack usage in mmc_ioctl(), dvd_read_disckey(), and
+  dvd_read_manufact.
+  -- Added setup of write mode for packet writing.
+  -- Fixed CDDA ripping with cdda2wav - accept much larger requests of
+  number of frames and split the reads in blocks of 8.
+
+  3.06 Dec 13, 1999 - Jens Axboe <axboe@image.dk>
+  -- Added support for changing the region of DVD drives.
+  -- Added sense data to generic command.
+
+  3.07 Feb 2, 2000 - Jens Axboe <axboe@suse.de>
+  -- Do same "read header length" trick in cdrom_get_disc_info() as
+  we do in cdrom_get_track_info() -- some drive don't obey specs and
+  fail if they can't supply the full Mt Fuji size table.
+  -- Deleted stuff related to setting up write modes. It has a different
+  home now.
+  -- Clear header length in mode_select unconditionally.
+  -- Removed the register_disk() that was added, not needed here.
+
+  3.08 May 1, 2000 - Jens Axboe <axboe@suse.de>
+  -- Fix direction flag in setup_send_key and setup_report_key. This
+  gave some SCSI adapters problems.
+  -- Always return -EROFS for write opens
+  -- Convert to module_init/module_exit style init and remove some
+  of the #ifdef MODULE stuff
+  -- Fix several dvd errors - DVD_LU_SEND_ASF should pass agid,
+  DVD_HOST_SEND_RPC_STATE did not set buffer size in cdb, and
+  dvd_do_auth passed uninitialized data to drive because init_cdrom_command
+  did not clear a 0 sized buffer.
+  
+  3.09 May 12, 2000 - Jens Axboe <axboe@suse.de>
+  -- Fix Video-CD on SCSI drives that don't support READ_CD command. In
+  that case switch block size and issue plain READ_10 again, then switch
+  back.
+
+  3.10 Jun 10, 2000 - Jens Axboe <axboe@suse.de>
+  -- Fix volume control on CD's - old SCSI-II drives now use their own
+  code, as doing MODE6 stuff in here is really not my intention.
+  -- Use READ_DISC_INFO for more reliable end-of-disc.
+
+  3.11 Jun 12, 2000 - Jens Axboe <axboe@suse.de>
+  -- Fix bug in getting rpc phase 2 region info.
+  -- Reinstate "correct" CDROMPLAYTRKIND
+
+   3.12 Oct 18, 2000 - Jens Axboe <axboe@suse.de>
+  -- Use quiet bit on packet commands not known to work
+
+-------------------------------------------------------------------------*/
+
+#define REVISION "Revision: 3.12"
+#define VERSION "Id: cdrom.c 3.12 2000/10/18"
+
+/* I use an error-log mask to give fine grain control over the type of
+   messages dumped to the system logs.  The available masks include: */
+#define CD_NOTHING      0x0
+#define CD_WARNING     0x1
+#define CD_REG_UNREG   0x2
+#define CD_DO_IOCTL    0x4
+#define CD_OPEN                0x8
+#define CD_CLOSE       0x10
+#define CD_COUNT_TRACKS 0x20
+#define CD_CHANGER     0x40
+#define CD_DVD         0x80
+
+/* Define this to remove _all_ the debugging messages */
+/* #define ERRLOGMASK CD_NOTHING */
+#define ERRLOGMASK (CD_WARNING)
+/* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
+/* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
+
+#include <xeno/config.h>
+#include <xeno/module.h>
+/* #include <xeno/fs.h> */
+#include <xeno/major.h>
+#include <xeno/types.h>
+#include <xeno/errno.h>
+#include <xeno/kernel.h>
+#include <xeno/mm.h>
+#include <xeno/slab.h> 
+#include <xeno/cdrom.h>
+/* #include <xeno/sysctl.h> */
+/* #include <xeno/proc_fs.h> */
+#include <xeno/init.h>
+
+/* #include <asm/fcntl.h> */
+/* #include <asm/segment.h> */
+#include <asm/uaccess.h>
+
+/* used to tell the module to turn on full debugging messages */
+static int debug;
+/* used to keep tray locked at all times */
+static int keeplocked;
+/* default compatibility mode */
+static int autoclose=1;
+static int autoeject;
+static int lockdoor = 1;
+/* will we ever get to use this... sigh. */
+static int check_media_type;
+MODULE_PARM(debug, "i");
+MODULE_PARM(autoclose, "i");
+MODULE_PARM(autoeject, "i");
+MODULE_PARM(lockdoor, "i");
+MODULE_PARM(check_media_type, "i");
+
+#if (ERRLOGMASK!=CD_NOTHING)
+#define cdinfo(type, fmt, args...) \
+        if ((ERRLOGMASK & type) || debug==1 ) \
+            printk(KERN_INFO "cdrom: " fmt, ## args)
+#else
+#define cdinfo(type, fmt, args...) 
+#endif
+
+/* These are used to simplify getting data in from and back to user land */
+#define IOCTL_IN(arg, type, in)                                        \
+       if (copy_from_user(&(in), (type *) (arg), sizeof (in))) \
+               return -EFAULT;
+
+#define IOCTL_OUT(arg, type, out) \
+       if (copy_to_user((type *) (arg), &(out), sizeof (out))) \
+               return -EFAULT;
+
+/* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
+   a lot of places. This macro makes the code more clear. */
+#define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
+
+/* used in the audio ioctls */
+#define CHECKAUDIO if ((ret=check_for_audio_disc(cdi, cdo))) return ret
+
+/* Not-exported routines. */
+static int open_for_data(struct cdrom_device_info * cdi);
+static int check_for_audio_disc(struct cdrom_device_info * cdi,
+                        struct cdrom_device_ops * cdo);
+static void sanitize_format(union cdrom_addr *addr, 
+               u_char * curr, u_char requested);
+static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
+                    unsigned long arg);
+
+int cdrom_get_last_written(kdev_t dev, long *last_written);
+int cdrom_get_next_writable(kdev_t dev, long *next_writable);
+
+#ifdef CONFIG_SYSCTL
+static void cdrom_sysctl_register(void);
+#endif /* CONFIG_SYSCTL */ 
+static struct cdrom_device_info *topCdromPtr;
+#if 0
+static devfs_handle_t devfs_handle;
+static struct unique_numspace cdrom_numspace = UNIQUE_NUMBERSPACE_INITIALISER;
+#endif
+
+/* This macro makes sure we don't have to check on cdrom_device_ops
+ * existence in the run-time routines below. Change_capability is a
+ * hack to have the capability flags defined const, while we can still
+ * change it here without gcc complaining at every line.
+ */
+#define ENSURE(call, bits) if (cdo->call == NULL) *change_capability &= ~(bits)
+
+int register_cdrom(struct cdrom_device_info *cdi)
+{
+       static char banner_printed;
+       int major = MAJOR(cdi->dev);
+        struct cdrom_device_ops *cdo = cdi->ops;
+        int *change_capability = (int *)&cdo->capability; /* hack */
+
+       cdinfo(CD_OPEN, "entering register_cdrom\n"); 
+
+       if (major < 0 || major >= MAX_BLKDEV)
+               return -1;
+       if (cdo->open == NULL || cdo->release == NULL)
+               return -2;
+       if ( !banner_printed ) {
+               printk(KERN_INFO "Uniform CD-ROM driver " REVISION "\n");
+               banner_printed = 1;
+#ifdef CONFIG_SYSCTL
+               cdrom_sysctl_register();
+#endif /* CONFIG_SYSCTL */ 
+       }
+       ENSURE(drive_status, CDC_DRIVE_STATUS );
+       ENSURE(media_changed, CDC_MEDIA_CHANGED);
+       ENSURE(tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
+       ENSURE(lock_door, CDC_LOCK);
+       ENSURE(select_speed, CDC_SELECT_SPEED);
+       ENSURE(get_last_session, CDC_MULTI_SESSION);
+       ENSURE(get_mcn, CDC_MCN);
+       ENSURE(reset, CDC_RESET);
+       ENSURE(audio_ioctl, CDC_PLAY_AUDIO);
+       ENSURE(dev_ioctl, CDC_IOCTLS);
+       ENSURE(generic_packet, CDC_GENERIC_PACKET);
+       cdi->mc_flags = 0;
+       cdo->n_minors = 0;
+        cdi->options = CDO_USE_FFLAGS;
+       
+       if (autoclose==1 && CDROM_CAN(CDC_CLOSE_TRAY))
+               cdi->options |= (int) CDO_AUTO_CLOSE;
+       if (autoeject==1 && CDROM_CAN(CDC_OPEN_TRAY))
+               cdi->options |= (int) CDO_AUTO_EJECT;
+       if (lockdoor==1)
+               cdi->options |= (int) CDO_LOCK;
+       if (check_media_type==1)
+               cdi->options |= (int) CDO_CHECK_TYPE;
+#if 0
+       if (!devfs_handle)
+               devfs_handle = devfs_mk_dir (NULL, "cdroms", NULL);
+       cdi->number = devfs_alloc_unique_number (&cdrom_numspace);
+       if (cdi->de) {
+               int pos;
+               devfs_handle_t slave;
+               char rname[64];
+
+               pos = devfs_generate_path (cdi->de, rname + 3,
+                                          sizeof rname - 3);
+               if (pos >= 0) {
+                       char vname[16];
+                       sprintf (vname, "cdrom%d", cdi->number);
+                       strncpy (rname + pos, "../", 3);
+                       devfs_mk_symlink (devfs_handle, vname,
+                                         DEVFS_FL_DEFAULT,
+                                         rname + pos, &slave, NULL);
+                       devfs_auto_unregister (cdi->de, slave);
+               }
+       }
+#endif
+       cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
+       cdi->next = topCdromPtr;        
+       topCdromPtr = cdi;
+       return 0;
+}
+#undef ENSURE
+
+int unregister_cdrom(struct cdrom_device_info *unreg)
+{
+       struct cdrom_device_info *cdi, *prev;
+       int major = MAJOR(unreg->dev);
+
+       cdinfo(CD_OPEN, "entering unregister_cdrom\n"); 
+
+       if (major < 0 || major >= MAX_BLKDEV)
+               return -1;
+
+       prev = NULL;
+       cdi = topCdromPtr;
+       while (cdi != NULL && cdi->dev != unreg->dev) {
+               prev = cdi;
+               cdi = cdi->next;
+       }
+
+       if (cdi == NULL)
+               return -2;
+       if (prev)
+               prev->next = cdi->next;
+       else
+               topCdromPtr = cdi->next;
+       cdi->ops->n_minors--;
+#if 0
+       devfs_unregister (cdi->de);
+       devfs_dealloc_unique_number (&cdrom_numspace, cdi->number);
+#endif
+       cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
+       return 0;
+}
+
+struct cdrom_device_info *cdrom_find_device(kdev_t dev)
+{
+       struct cdrom_device_info *cdi;
+
+       cdi = topCdromPtr;
+       while (cdi != NULL && cdi->dev != dev)
+               cdi = cdi->next;
+
+       return cdi;
+}
+
+/* We use the open-option O_NONBLOCK to indicate that the
+ * purpose of opening is only for subsequent ioctl() calls; no device
+ * integrity checks are performed.
+ *
+ * We hope that all cd-player programs will adopt this convention. It
+ * is in their own interest: device control becomes a lot easier
+ * this way.
+ */
+int cdrom_open(struct inode *ip, struct file *fp)
+{
+       struct cdrom_device_info *cdi;
+       kdev_t dev = ip->i_rdev;
+       int ret;
+
+       cdinfo(CD_OPEN, "entering cdrom_open\n"); 
+       if ((cdi = cdrom_find_device(dev)) == NULL)
+               return -ENODEV;
+
+#if 0
+       if ((fp->f_mode & FMODE_WRITE) && !CDROM_CAN(CDC_DVD_RAM))
+               return -EROFS;
+
+       /* if this was a O_NONBLOCK open and we should honor the flags,
+        * do a quick open without drive/disc integrity checks. */
+       if ((fp->f_flags & O_NONBLOCK) && (cdi->options & CDO_USE_FFLAGS))
+               ret = cdi->ops->open(cdi, 1);
+       else
+               ret = open_for_data(cdi);
+#else 
+       ret = open_for_data(cdi);
+#endif
+
+       if (!ret) cdi->use_count++;
+
+       cdinfo(CD_OPEN, "Use count for \"/dev/%s\" now %d\n", cdi->name, cdi->use_count);
+       /* Do this on open.  Don't wait for mount, because they might
+           not be mounting, but opening with O_NONBLOCK */
+       check_disk_change(dev);
+       return ret;
+}
+
+static
+int open_for_data(struct cdrom_device_info * cdi)
+{
+       int ret;
+       struct cdrom_device_ops *cdo = cdi->ops;
+       tracktype tracks;
+       cdinfo(CD_OPEN, "entering open_for_data\n");
+       /* Check if the driver can report drive status.  If it can, we
+          can do clever things.  If it can't, well, we at least tried! */
+       if (cdo->drive_status != NULL) {
+               ret = cdo->drive_status(cdi, CDSL_CURRENT);
+               cdinfo(CD_OPEN, "drive_status=%d\n", ret); 
+               if (ret == CDS_TRAY_OPEN) {
+                       cdinfo(CD_OPEN, "the tray is open...\n"); 
+                       /* can/may i close it? */
+                       if (CDROM_CAN(CDC_CLOSE_TRAY) &&
+                           cdi->options & CDO_AUTO_CLOSE) {
+                               cdinfo(CD_OPEN, "trying to close the tray.\n"); 
+                               ret=cdo->tray_move(cdi,0);
+                               if (ret) {
+                                       cdinfo(CD_OPEN, "bummer. tried to close the tray but failed.\n"); 
+                                       /* Ignore the error from the low
+                                       level driver.  We don't care why it
+                                       couldn't close the tray.  We only care 
+                                       that there is no disc in the drive, 
+                                       since that is the _REAL_ problem here.*/
+                                       ret=-ENOMEDIUM;
+                                       goto clean_up_and_return;
+                               }
+                       } else {
+                               cdinfo(CD_OPEN, "bummer. this drive can't close the tray.\n"); 
+                               ret=-ENOMEDIUM;
+                               goto clean_up_and_return;
+                       }
+                       /* Ok, the door should be closed now.. Check again */
+                       ret = cdo->drive_status(cdi, CDSL_CURRENT);
+                       if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
+                               cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n"); 
+                               cdinfo(CD_OPEN, "tray might not contain a medium.\n");
+                               ret=-ENOMEDIUM;
+                               goto clean_up_and_return;
+                       }
+                       cdinfo(CD_OPEN, "the tray is now closed.\n"); 
+               }
+               if (ret!=CDS_DISC_OK) {
+                       ret = -ENOMEDIUM;
+                       goto clean_up_and_return;
+               }
+       }
+       cdrom_count_tracks(cdi, &tracks);
+       if (tracks.error == CDS_NO_DISC) {
+               cdinfo(CD_OPEN, "bummer. no disc.\n");
+               ret=-ENOMEDIUM;
+               goto clean_up_and_return;
+       }
+       /* CD-Players which don't use O_NONBLOCK, workman
+        * for example, need bit CDO_CHECK_TYPE cleared! */
+       if (tracks.data==0) {
+               if (cdi->options & CDO_CHECK_TYPE) {
+                   /* give people a warning shot, now that CDO_CHECK_TYPE
+                      is the default case! */
+                   cdinfo(CD_OPEN, "bummer. wrong media type.\n"); 
+#if 0
+                   cdinfo(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
+                                       (unsigned int)current->pid); 
+#else
+                   cdinfo(CD_WARNING, "xen must open device O_NONBLOCK!\n"); 
+#endif
+                   ret=-EMEDIUMTYPE;
+                   goto clean_up_and_return;
+               }
+               else {
+                   cdinfo(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set.\n");
+               }
+       }
+
+       cdinfo(CD_OPEN, "all seems well, opening the device.\n"); 
+
+       /* all seems well, we can open the device */
+       ret = cdo->open(cdi, 0); /* open for data */
+       cdinfo(CD_OPEN, "opening the device gave me %d.\n", ret); 
+       /* After all this careful checking, we shouldn't have problems
+          opening the device, but we don't want the device locked if 
+          this somehow fails... */
+       if (ret) {
+               cdinfo(CD_OPEN, "open device failed.\n"); 
+               goto clean_up_and_return;
+       }
+       if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
+                       cdo->lock_door(cdi, 1);
+                       cdinfo(CD_OPEN, "door locked.\n");
+       }
+       cdinfo(CD_OPEN, "device opened successfully.\n"); 
+       return ret;
+
+       /* Something failed.  Try to unlock the drive, because some drivers
+       (notably ide-cd) lock the drive after every command.  This produced
+       a nasty bug where after mount failed, the drive would remain locked!  
+       This ensures that the drive gets unlocked after a mount fails.  This 
+       is a goto to avoid bloating the driver with redundant code. */ 
+clean_up_and_return:
+       cdinfo(CD_WARNING, "open failed.\n"); 
+       if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
+                       cdo->lock_door(cdi, 0);
+                       cdinfo(CD_OPEN, "door unlocked.\n");
+       }
+       return ret;
+}
+
+/* This code is similar to that in open_for_data. The routine is called
+   whenever an audio play operation is requested.
+*/
+int check_for_audio_disc(struct cdrom_device_info * cdi,
+                        struct cdrom_device_ops * cdo)
+{
+        int ret;
+       tracktype tracks;
+       cdinfo(CD_OPEN, "entering check_for_audio_disc\n");
+       if (!(cdi->options & CDO_CHECK_TYPE))
+               return 0;
+       if (cdo->drive_status != NULL) {
+               ret = cdo->drive_status(cdi, CDSL_CURRENT);
+               cdinfo(CD_OPEN, "drive_status=%d\n", ret); 
+               if (ret == CDS_TRAY_OPEN) {
+                       cdinfo(CD_OPEN, "the tray is open...\n"); 
+                       /* can/may i close it? */
+                       if (CDROM_CAN(CDC_CLOSE_TRAY) &&
+                           cdi->options & CDO_AUTO_CLOSE) {
+                               cdinfo(CD_OPEN, "trying to close the tray.\n"); 
+                               ret=cdo->tray_move(cdi,0);
+                               if (ret) {
+                                       cdinfo(CD_OPEN, "bummer. tried to close tray but failed.\n"); 
+                                       /* Ignore the error from the low
+                                       level driver.  We don't care why it
+                                       couldn't close the tray.  We only care 
+                                       that there is no disc in the drive, 
+                                       since that is the _REAL_ problem here.*/
+                                       return -ENOMEDIUM;
+                               }
+                       } else {
+                               cdinfo(CD_OPEN, "bummer. this driver can't close the tray.\n"); 
+                               return -ENOMEDIUM;
+                       }
+                       /* Ok, the door should be closed now.. Check again */
+                       ret = cdo->drive_status(cdi, CDSL_CURRENT);
+                       if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
+                               cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n"); 
+                               return -ENOMEDIUM;
+                       }       
+                       if (ret!=CDS_DISC_OK) {
+                               cdinfo(CD_OPEN, "bummer. disc isn't ready.\n"); 
+                               return -EIO;
+                       }       
+                       cdinfo(CD_OPEN, "the tray is now closed.\n"); 
+               }       
+       }
+       cdrom_count_tracks(cdi, &tracks);
+       if (tracks.error) 
+               return(tracks.error);
+
+       if (tracks.audio==0)
+               return -EMEDIUMTYPE;
+
+       return 0;
+}
+
+
+/* Admittedly, the logic below could be performed in a nicer way. */
+int cdrom_release(struct inode *ip, struct file *fp)
+{
+       kdev_t dev = ip->i_rdev;
+       struct cdrom_device_info *cdi = cdrom_find_device(dev);
+       struct cdrom_device_ops *cdo = cdi->ops;
+       int opened_for_data;
+
+       cdinfo(CD_CLOSE, "entering cdrom_release\n"); 
+
+       if (cdi->use_count > 0)
+               cdi->use_count--;
+       if (cdi->use_count == 0)
+               cdinfo(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n", cdi->name);
+       if (cdi->use_count == 0 &&
+           cdo->capability & CDC_LOCK && !keeplocked) {
+               cdinfo(CD_CLOSE, "Unlocking door!\n");
+               cdo->lock_door(cdi, 0);
+       }
+#if 0
+       opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
+               !(fp && fp->f_flags & O_NONBLOCK);
+#else 
+       opened_for_data = 1; /* XXX SMH: guess */
+#endif
+       cdo->release(cdi);
+       if (cdi->use_count == 0) {      /* last process that closes dev*/
+               if (opened_for_data &&
+                   cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
+                       cdo->tray_move(cdi, 1);
+       }
+       return 0;
+}
+
+static int cdrom_read_mech_status(struct cdrom_device_info *cdi, 
+                                 struct cdrom_changer_info *buf)
+{
+       struct cdrom_generic_command cgc;
+       struct cdrom_device_ops *cdo = cdi->ops;
+       int length;
+
+       /*
+        * Sanyo changer isn't spec compliant (doesn't use regular change
+        * LOAD_UNLOAD command, and it doesn't implement the mech status
+        * command below
+        */
+       if (cdi->sanyo_slot) {
+               buf->hdr.nslots = 3;
+               buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
+               for (length = 0; length < 3; length++) {
+                       buf->slots[length].disc_present = 1;
+                       buf->slots[length].change = 0;
+               }
+               return 0;
+       }
+
+       length = sizeof(struct cdrom_mechstat_header) +
+                cdi->capacity * sizeof(struct cdrom_slot);
+
+       init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
+       cgc.cmd[8] = (length >> 8) & 0xff;
+       cgc.cmd[9] = length & 0xff;
+       return cdo->generic_packet(cdi, &cgc);
+}
+
+static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
+{
+       struct cdrom_changer_info info;
+       int ret;
+
+       cdinfo(CD_CHANGER, "entering cdrom_slot_status()\n"); 
+       if (cdi->sanyo_slot)
+               return CDS_NO_INFO;
+       
+       if ((ret = cdrom_read_mech_status(cdi, &info)))
+               return ret;
+
+       if (info.slots[slot].disc_present)
+               return CDS_DISC_OK;
+       else
+               return CDS_NO_DISC;
+
+}
+
+/* Return the number of slots for an ATAPI/SCSI cdrom, 
+ * return 1 if not a changer. 
+ */
+int cdrom_number_of_slots(struct cdrom_device_info *cdi) 
+{
+       int status;
+       int nslots = 1;
+       struct cdrom_changer_info info;
+
+       cdinfo(CD_CHANGER, "entering cdrom_number_of_slots()\n"); 
+       /* cdrom_read_mech_status requires a valid value for capacity: */
+       cdi->capacity = 0; 
+
+       if ((status = cdrom_read_mech_status(cdi, &info)) == 0)
+               nslots = info.hdr.nslots;
+
+       return nslots;
+}
+
+
+/* If SLOT < 0, unload the current slot.  Otherwise, try to load SLOT. */
+static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot) 
+{
+       struct cdrom_generic_command cgc;
+
+       cdinfo(CD_CHANGER, "entering cdrom_load_unload()\n"); 
+       if (cdi->sanyo_slot && slot < 0)
+               return 0;
+
+       init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
+       cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
+       cgc.cmd[4] = 2 + (slot >= 0);
+       cgc.cmd[8] = slot;
+
+       /* The Sanyo 3 CD changer uses byte 7 of the 
+       GPCMD_TEST_UNIT_READY to command to switch CDs instead of
+       using the GPCMD_LOAD_UNLOAD opcode. */
+       if (cdi->sanyo_slot && -1 < slot) {
+               cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
+               cgc.cmd[7] = slot;
+               cgc.cmd[4] = cgc.cmd[8] = 0;
+               cdi->sanyo_slot = slot ? slot : 3;
+       }
+
+       return cdi->ops->generic_packet(cdi, &cgc);
+}
+
+int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
+{
+       struct cdrom_changer_info info;
+       int curslot;
+       int ret;
+
+       cdinfo(CD_CHANGER, "entering cdrom_select_disc()\n"); 
+       if (!CDROM_CAN(CDC_SELECT_DISC))
+               return -EDRIVE_CANT_DO_THIS;
+
+       (void) cdi->ops->media_changed(cdi, slot);
+
+       if (slot == CDSL_NONE) {
+               /* set media changed bits, on both queues */
+               cdi->mc_flags = 0x3;
+               return cdrom_load_unload(cdi, -1);
+       }
+
+       if ((ret = cdrom_read_mech_status(cdi, &info)))
+               return ret;
+
+       curslot = info.hdr.curslot;
+
+       if (cdi->use_count > 1 || keeplocked) {
+               if (slot == CDSL_CURRENT) {
+                       return curslot;
+               } else {
+                       return -EBUSY;
+               }
+       }
+
+       /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
+       which is useful if it had been previously unloaded.
+       Whether it can or not, it returns the current slot. 
+       Similarly,  if slot happens to be the current one, we still
+       try and load it. */
+       if (slot == CDSL_CURRENT)
+               slot = curslot;
+
+       /* set media changed bits on both queues */
+       cdi->mc_flags = 0x3;
+       if ((ret = cdrom_load_unload(cdi, slot)))
+               return ret;
+
+       return slot;
+}
+
+/* We want to make media_changed accessible to the user through an
+ * ioctl. The main problem now is that we must double-buffer the
+ * low-level implementation, to assure that the VFS and the user both
+ * see a medium change once.
+ */
+
+static
+int media_changed(struct cdrom_device_info *cdi, int queue)
+{
+       unsigned int mask = (1 << (queue & 1));
+       int ret = !!(cdi->mc_flags & mask);
+
+       if (!CDROM_CAN(CDC_MEDIA_CHANGED))
+           return ret;
+       /* changed since last call? */
+       if (cdi->ops->media_changed(cdi, CDSL_CURRENT)) {
+               cdi->mc_flags = 0x3;    /* set bit on both queues */
+               ret |= 1;
+       }
+       cdi->mc_flags &= ~mask;         /* clear bit */
+       return ret;
+}
+
+int cdrom_media_changed(kdev_t dev)
+{
+       struct cdrom_device_info *cdi = cdrom_find_device(dev);
+       /* This talks to the VFS, which doesn't like errors - just 1 or 0.  
+        * Returning "0" is always safe (media hasn't been changed). Do that 
+        * if the low-level cdrom driver dosn't support media changed. */ 
+       if (cdi == NULL || cdi->ops->media_changed == NULL)
+               return 0;
+       if (!CDROM_CAN(CDC_MEDIA_CHANGED))
+               return 0;
+       return media_changed(cdi, 0);
+}
+
+/* badly broken, I know. Is due for a fixup anytime. */
+void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype* tracks)
+{
+       struct cdrom_tochdr header;
+       struct cdrom_tocentry entry;
+       int ret, i;
+       tracks->data=0;
+       tracks->audio=0;
+       tracks->cdi=0;
+       tracks->xa=0;
+       tracks->error=0;
+       cdinfo(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n"); 
+        if (!CDROM_CAN(CDC_PLAY_AUDIO)) { 
+                tracks->error=CDS_NO_INFO;
+                return;
+        }        
+       /* Grab the TOC header so we can see how many tracks there are */
+       if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header))) {
+               if (ret == -ENOMEDIUM)
+                       tracks->error = CDS_NO_DISC;
+               else
+                       tracks->error = CDS_NO_INFO;
+               return;
+       }       
+       /* check what type of tracks are on this disc */
+       entry.cdte_format = CDROM_MSF;
+       for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
+               entry.cdte_track  = i;
+               if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
+                       tracks->error=CDS_NO_INFO;
+                       return;
+               }       
+               if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
+                   if (entry.cdte_format == 0x10)
+                       tracks->cdi++;
+                   else if (entry.cdte_format == 0x20) 
+                       tracks->xa++;
+                   else
+                       tracks->data++;
+               } else
+                   tracks->audio++;
+               cdinfo(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
+                      i, entry.cdte_format, entry.cdte_ctrl);
+       }       
+       cdinfo(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n", 
+               header.cdth_trk1, tracks->audio, tracks->data, 
+               tracks->cdi, tracks->xa);
+}      
+
+/* Requests to the low-level drivers will /always/ be done in the
+   following format convention:
+
+   CDROM_LBA: all data-related requests.
+   CDROM_MSF: all audio-related requests.
+
+   However, a low-level implementation is allowed to refuse this
+   request, and return information in its own favorite format.
+
+   It doesn't make sense /at all/ to ask for a play_audio in LBA
+   format, or ask for multi-session info in MSF format. However, for
+   backward compatibility these format requests will be satisfied, but
+   the requests to the low-level drivers will be sanitized in the more
+   meaningful format indicated above.
+ */
+
+static
+void sanitize_format(union cdrom_addr *addr,
+                    u_char * curr, u_char requested)
+{
+       if (*curr == requested)
+               return;                 /* nothing to be done! */
+       if (requested == CDROM_LBA) {
+               addr->lba = (int) addr->msf.frame +
+                       75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
+       } else {                        /* CDROM_MSF */
+               int lba = addr->lba;
+               addr->msf.frame = lba % 75;
+               lba /= 75;
+               lba += 2;
+               addr->msf.second = lba % 60;
+               addr->msf.minute = lba / 60;
+       }
+       *curr = requested;
+}
+
+void init_cdrom_command(struct cdrom_generic_command *cgc, void *buf, int len,
+                       int type)
+{
+       memset(cgc, 0, sizeof(struct cdrom_generic_command));
+       if (buf)
+               memset(buf, 0, len);
+       cgc->buffer = (char *) buf;
+       cgc->buflen = len;
+       cgc->data_direction = type;
+       cgc->timeout = 5*HZ;
+}
+
+/* DVD handling */
+
+#define copy_key(dest,src)     memcpy((dest), (src), sizeof(dvd_key))
+#define copy_chal(dest,src)    memcpy((dest), (src), sizeof(dvd_challenge))
+
+static void setup_report_key(struct cdrom_generic_command *cgc, unsigned agid, unsigned type)
+{
+       cgc->cmd[0] = GPCMD_REPORT_KEY;
+       cgc->cmd[10] = type | (agid << 6);
+       switch (type) {
+               case 0: case 8: case 5: {
+                       cgc->buflen = 8;
+                       break;
+               }
+               case 1: {
+                       cgc->buflen = 16;
+                       break;
+               }
+               case 2: case 4: {
+                       cgc->buflen = 12;
+                       break;
+               }
+       }
+       cgc->cmd[9] = cgc->buflen;
+       cgc->data_direction = CGC_DATA_READ;
+}
+
+static void setup_send_key(struct cdrom_generic_command *cgc, unsigned agid, unsigned type)
+{
+       cgc->cmd[0] = GPCMD_SEND_KEY;
+       cgc->cmd[10] = type | (agid << 6);
+       switch (type) {
+               case 1: {
+                       cgc->buflen = 16;
+                       break;
+               }
+               case 3: {
+                       cgc->buflen = 12;
+                       break;
+               }
+               case 6: {
+                       cgc->buflen = 8;
+                       break;
+               }
+       }
+       cgc->cmd[9] = cgc->buflen;
+       cgc->data_direction = CGC_DATA_WRITE;
+}
+
+static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
+{
+       int ret;
+       u_char buf[20];
+       struct cdrom_generic_command cgc;
+       struct cdrom_device_ops *cdo = cdi->ops;
+       rpc_state_t rpc_state;
+
+       memset(buf, 0, sizeof(buf));
+       init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
+
+       switch (ai->type) {
+       /* LU data send */
+       case DVD_LU_SEND_AGID:
+               cdinfo(CD_DVD, "entering DVD_LU_SEND_AGID\n"); 
+               setup_report_key(&cgc, ai->lsa.agid, 0);
+
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+
+               ai->lsa.agid = buf[7] >> 6;
+               /* Returning data, let host change state */
+               break;
+
+       case DVD_LU_SEND_KEY1:
+               cdinfo(CD_DVD, "entering DVD_LU_SEND_KEY1\n"); 
+               setup_report_key(&cgc, ai->lsk.agid, 2);
+
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+
+               copy_key(ai->lsk.key, &buf[4]);
+               /* Returning data, let host change state */
+               break;
+
+       case DVD_LU_SEND_CHALLENGE:
+               cdinfo(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n"); 
+               setup_report_key(&cgc, ai->lsc.agid, 1);
+
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+
+               copy_chal(ai->lsc.chal, &buf[4]);
+               /* Returning data, let host change state */
+               break;
+
+       /* Post-auth key */
+       case DVD_LU_SEND_TITLE_KEY:
+               cdinfo(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n"); 
+               setup_report_key(&cgc, ai->lstk.agid, 4);
+               cgc.cmd[5] = ai->lstk.lba;
+               cgc.cmd[4] = ai->lstk.lba >> 8;
+               cgc.cmd[3] = ai->lstk.lba >> 16;
+               cgc.cmd[2] = ai->lstk.lba >> 24;
+
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+
+               ai->lstk.cpm = (buf[4] >> 7) & 1;
+               ai->lstk.cp_sec = (buf[4] >> 6) & 1;
+               ai->lstk.cgms = (buf[4] >> 4) & 3;
+               copy_key(ai->lstk.title_key, &buf[5]);
+               /* Returning data, let host change state */
+               break;
+
+       case DVD_LU_SEND_ASF:
+               cdinfo(CD_DVD, "entering DVD_LU_SEND_ASF\n"); 
+               setup_report_key(&cgc, ai->lsasf.agid, 5);
+               
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+
+               ai->lsasf.asf = buf[7] & 1;
+               break;
+
+       /* LU data receive (LU changes state) */
+       case DVD_HOST_SEND_CHALLENGE:
+               cdinfo(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n"); 
+               setup_send_key(&cgc, ai->hsc.agid, 1);
+               buf[1] = 0xe;
+               copy_chal(&buf[4], ai->hsc.chal);
+
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+
+               ai->type = DVD_LU_SEND_KEY1;
+               break;
+
+       case DVD_HOST_SEND_KEY2:
+               cdinfo(CD_DVD, "entering DVD_HOST_SEND_KEY2\n"); 
+               setup_send_key(&cgc, ai->hsk.agid, 3);
+               buf[1] = 0xa;
+               copy_key(&buf[4], ai->hsk.key);
+
+               if ((ret = cdo->generic_packet(cdi, &cgc))) {
+                       ai->type = DVD_AUTH_FAILURE;
+                       return ret;
+               }
+               ai->type = DVD_AUTH_ESTABLISHED;
+               break;
+
+       /* Misc */
+       case DVD_INVALIDATE_AGID:
+               cdinfo(CD_DVD, "entering DVD_INVALIDATE_AGID\n"); 
+               setup_report_key(&cgc, ai->lsa.agid, 0x3f);
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+               break;
+
+       /* Get region settings */
+       case DVD_LU_SEND_RPC_STATE:
+               cdinfo(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
+               setup_report_key(&cgc, 0, 8);
+               memset(&rpc_state, 0, sizeof(rpc_state_t));
+               cgc.buffer = (char *) &rpc_state;
+
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+
+               ai->lrpcs.type = rpc_state.type_code;
+               ai->lrpcs.vra = rpc_state.vra;
+               ai->lrpcs.ucca = rpc_state.ucca;
+               ai->lrpcs.region_mask = rpc_state.region_mask;
+               ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
+               break;
+
+       /* Set region settings */
+       case DVD_HOST_SEND_RPC_STATE:
+               cdinfo(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
+               setup_send_key(&cgc, 0, 6);
+               buf[1] = 6;
+               buf[4] = ai->hrpcs.pdrc;
+
+               if ((ret = cdo->generic_packet(cdi, &cgc)))
+                       return ret;
+               break;
+
+       default:
+               cdinfo(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
+               return -ENOTTY;
+       }
+
+       return 0;
+}
+
+static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s)
+{
+       unsigned char buf[20], *base;
+       struct dvd_layer *layer;
+       struct cdrom_generic_command cgc;
+       struct cdrom_device_ops *cdo = cdi->ops;
+       int ret, layer_num = s->physical.layer_num;
+
+       if (layer_num >= DVD_LAYERS)
+               return -EINVAL;
+
+       init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
+       cgc.cmd[6] = layer_num;
+       cgc.cmd[7] = s->type;
+       cgc.cmd[9] = cgc.buflen & 0xff;
+
+       /*
+        * refrain from reporting errors on non-existing layers (mainly)
+        */
+       cgc.quiet = 1;
+
+       if ((ret = cdo->generic_packet(cdi, &cgc)))
+               return ret;
+
+       base = &buf[4];
+       layer = &s->physical.layer[layer_num];
+
+       /*
+        * place the data... really ugly, but at least we won't have to
+        * worry about endianess in userspace.
+        */
+       memset(layer, 0, sizeof(*layer));
+       layer->book_version = base[0] & 0xf;
+       layer->book_type = base[0] >> 4;
+       layer->min_rate = base[1] & 0xf;
+       layer->disc_size = base[1] >> 4;
+       layer->layer_type = base[2] & 0xf;
+       layer->track_path = (base[2] >> 4) & 1;
+       layer->nlayers = (base[2] >> 5) & 3;
+       layer->track_density = base[3] & 0xf;
+       layer->linear_density = base[3] >> 4;
+       layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
+       layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
+       layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
+       layer->bca = base[16] >> 7;
+
+       return 0;
+}
+
+static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s)
+{
+       int ret;
+       u_char buf[8];
+       struct cdrom_generic_command cgc;
+       struct cdrom_device_ops *cdo = cdi->ops;
+
+       init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
+       cgc.cmd[6] = s->copyright.layer_num;
+       cgc.cmd[7] = s->type;
+       cgc.cmd[8] = cgc.buflen >> 8;
+       cgc.cmd[9] = cgc.buflen & 0xff;
+
+       if ((ret = cdo->generic_packet(cdi, &cgc)))
+               return ret;
+
+       s->copyright.cpst = buf[4];
+       s->copyright.rmi = buf[5];
+
+       return 0;
+}
+
+static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s)
+{
+       int ret, size;
+       u_char *buf;
+       struct cdrom_generic_command cgc;
+       struct cdrom_device_ops *cdo = cdi->ops;
+
+       size = sizeof(s->disckey.value) + 4;
+
+       if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
+               return -ENOMEM;
+
+       init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
+       cgc.cmd[7] = s->type;
+       cgc.cmd[8] = size >> 8;
+       cgc.cmd[9] = size & 0xff;
+       cgc.cmd[10] = s->disckey.agid << 6;
+
+       if (!(ret = cdo->generic_packet(cdi, &cgc)))
+               memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
+
+       kfree(buf);
+       return ret;
+}
+
+static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s)
+{
+       int ret;
+       u_char buf[4 + 188];
+       struct cdrom_generic_command cgc;
+       struct cdrom_device_ops *cdo = cdi->ops;
+
+       init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
+       cgc.cmd[7] = s->type;
+       cgc.cmd[9] = cgc.buflen = 0xff;
+
+       if ((ret = cdo->generic_packet(cdi, &cgc)))
+               return ret;
+
+       s->bca.len = buf[0] << 8 | buf[1];
+       if (s->bca.len < 12 || s->bca.len > 188) {
+               cdinfo(CD_WARNING, "Received invalid BCA length (%d)\n", s->bca.len);
+               return -EIO;
+       }
+       memcpy(s->bca.value, &buf[4], s->bca.len);
+
+       return 0;
+}
+
+static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s)
+{
+       int ret = 0, size;
+       u_char *buf;
+       struct cdrom_generic_command cgc;
+       struct cdrom_device_ops *cdo = cdi->ops;
+
+       size = sizeof(s->manufact.value) + 4;
+
+       if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
+               return -ENOMEM;
+
+       init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
+       cgc.cmd[7] = s->type;
+       cgc.cmd[8] = size >> 8;
+       cgc.cmd[9] = size & 0xff;
+
+       if ((ret = cdo->generic_packet(cdi, &cgc))) {
+               kfree(buf);
+               return ret;
+       }
+
+       s->manufact.len = buf[0] << 8 | buf[1];
+       if (s->manufact.len < 0 || s->manufact.len > 2048) {
+               cdinfo(CD_WARNING, "Received invalid manufacture info length"
+                                  " (%d)\n", s->bca.len);
+               ret = -EIO;
+       } else {
+               memcpy(s->manufact.value, &buf[4], s->manufact.len);
+       }
+
+       kfree(buf);
+       return ret;
+}
+
+static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s)
+{
+       switch (s->type) {
+       case DVD_STRUCT_PHYSICAL:
+               return dvd_read_physical(cdi, s);
+
+       case DVD_STRUCT_COPYRIGHT:
+               return dvd_read_copyright(cdi, s);
+
+       case DVD_STRUCT_DISCKEY:
+               return dvd_read_disckey(cdi, s);
+
+       case DVD_STRUCT_BCA:
+               return dvd_read_bca(cdi, s);
+
+       case DVD_STRUCT_MANUFACT:
+               return dvd_read_manufact(cdi, s);
+               
+       default:
+               cdinfo(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
+                                       s->type);
+               return -EINVAL;
+       }
+}
+
+int cdrom_mode_sense(struct cdrom_device_info *cdi,
+                    struct cdrom_generic_command *cgc,
+                    int page_code, int page_control)
+{
+       struct cdrom_device_ops *cdo = cdi->ops;
+
+       memset(cgc->cmd, 0, sizeof(cgc->cmd));
+
+       cgc->cmd[0] = GPCMD_MODE_SENSE_10;
+       cgc->cmd[2] = page_code | (page_control << 6);
+       cgc->cmd[7] = cgc->buflen >> 8;
+       cgc->cmd[8] = cgc->buflen & 0xff;
+       cgc->data_direction = CGC_DATA_READ;
+       return cdo->generic_packet(cdi, cgc);
+}
+
+int cdrom_mode_select(struct cdrom_device_info *cdi,
+                     struct cdrom_generic_command *cgc)
+{
+       struct cdrom_device_ops *cdo = cdi->ops;
+
+       memset(cgc->cmd, 0, sizeof(cgc->cmd));
+       memset(cgc->buffer, 0, 2);
+       cgc->cmd[0] = GPCMD_MODE_SELECT_10;
+       cgc->cmd[1] = 0x10;             /* PF */
+       cgc->cmd[7] = cgc->buflen >> 8;
+       cgc->cmd[8] = cgc->buflen & 0xff;
+       cgc->data_direction = CGC_DATA_WRITE;
+       return cdo->generic_packet(cdi, cgc);
+}
+
+static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
+                                struct cdrom_subchnl *subchnl, int mcn)
+{
+       struct cdrom_device_ops *cdo = cdi->ops;
+       struct cdrom_generic_command cgc;
+       char buffer[32];
+       int ret;
+
+       init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
+       cgc.cmd[1] = 2;     /* MSF addressing */
+       cgc.cmd[2] = 0x40;  /* request subQ data */
+       cgc.cmd[3] = mcn ? 2 : 1;
+       cgc.cmd[8] = 16;
+
+       if ((ret = cdo->generic_packet(cdi, &cgc)))
+               return ret;
+
+       subchnl->cdsc_audiostatus = cgc.buffer[1];
+       subchnl->cdsc_format = CDROM_MSF;
+       subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
+       subchnl->cdsc_trk = cgc.buffer[6];
+       subchnl->cdsc_ind = cgc.buffer[7];
+
+       subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
+       subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
+       subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
+       subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
+       subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
+       subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
+
+       return 0;
+}
+
+/*
+ * Specific READ_10 interface
+ */
+static int cdrom_read_cd(struct cdrom_device_info *cdi,
+                        struct cdrom_generic_command *cgc, int lba,
+                        int blocksize, int nblocks)
+{
+       struct cdrom_device_ops *cdo = cdi->ops;
+
+       memset(&cgc->cmd, 0, sizeof(cgc->cmd));
+       cgc->cmd[0] = GPCMD_READ_10;
+       cgc->cmd[2] = (lba >> 24) & 0xff;
+       cgc->cmd[3] = (lba >> 16) & 0xff;
+       cgc->cmd[4] = (lba >>  8) & 0xff;
+       cgc->cmd[5] = lba & 0xff;
+       cgc->cmd[6] = (nblocks >> 16) & 0xff;
+       cgc->cmd[7] = (nblocks >>  8) & 0xff;
+       cgc->cmd[8] = nblocks & 0xff;
+       cgc->buflen = blocksize * nblocks;
+       return cdo->generic_packet(cdi, cgc);
+}
+
+/* very generic interface for reading the various types of blocks */
+static int cdrom_read_block(struct cdrom_device_info *cdi,
+                           struct cdrom_generic_command *cgc,
+                           int lba, int nblocks, int format, int blksize)
+{
+       struct cdrom_device_ops *cdo = cdi->ops;
+
+       memset(&cgc->cmd, 0, sizeof(cgc->cmd));
+       cgc->cmd[0] = GPCMD_READ_CD;
+       /* expected sector size - cdda,mode1,etc. */
+       cgc->cmd[1] = format << 2;
+       /* starting address */
+       cgc->cmd[2] = (lba >> 24) & 0xff;
+       cgc->cmd[3] = (lba >> 16) & 0xff;
+       cgc->cmd[4] = (lba >>  8) & 0xff;
+       cgc->cmd[5] = lba & 0xff;
+       /* number of blocks */
+       cgc->cmd[6] = (nblocks >> 16) & 0xff;
+       cgc->cmd[7] = (nblocks >>  8) & 0xff;
+       cgc->cmd[8] = nblocks & 0xff;
+       cgc->buflen = blksize * nblocks;
+       
+       /* set the header info returned */
+       switch (blksize) {
+       case CD_FRAMESIZE_RAW0  : cgc->cmd[9] = 0x58; break;
+       case CD_FRAMESIZE_RAW1  : cgc->cmd[9] = 0x78; break;
+       case CD_FRAMESIZE_RAW   : cgc->cmd[9] = 0xf8; break;
+       default                 : cgc->cmd[9] = 0x10;
+       }
+       
+       return cdo->generic_packet(cdi, cgc);
+}
+
+/* Just about every imaginable ioctl is supported in the Uniform layer
+ * these days. ATAPI / SCSI specific code now mainly resides in
+ * mmc_ioct().
+ */
+int cdrom_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
+                      unsigned long arg)
+{
+       kdev_t dev = ip->i_rdev;
+       struct cdrom_device_info *cdi = cdrom_find_device(dev);
+       struct cdrom_device_ops *cdo = cdi->ops;
+       int ret;
+
+       /* the first few commands do not deal with audio drive_info, but
+          only with routines in cdrom device operations. */
+       switch (cmd) {
+       case CDROMMULTISESSION: {
+               struct cdrom_multisession ms_info;
+               u_char requested_format;
+               cdinfo(CD_DO_IOCTL, "entering CDROMMULTISESSION\n"); 
+                if (!(cdo->capability & CDC_MULTI_SESSION))
+                        return -ENOSYS;
+               IOCTL_IN(arg, struct cdrom_multisession, ms_info);
+               requested_format = ms_info.addr_format;
+               if (!((requested_format == CDROM_MSF) ||
+                       (requested_format == CDROM_LBA)))
+                               return -EINVAL;
+               ms_info.addr_format = CDROM_LBA;
+               if ((ret=cdo->get_last_session(cdi, &ms_info)))
+                       return ret;
+               sanitize_format(&ms_info.addr, &ms_info.addr_format,
+                               requested_format);
+               IOCTL_OUT(arg, struct cdrom_multisession, ms_info);
+               cdinfo(CD_DO_IOCTL, "CDROMMULTISESSION successful\n"); 
+               return 0;
+               }
+
+       case CDROMEJECT: {
+               cdinfo(CD_DO_IOCTL, "entering CDROMEJECT\n"); 
+               if (!CDROM_CAN(CDC_OPEN_TRAY))
+                       return -ENOSYS;
+               if (cdi->use_count != 1 || keeplocked)
+                       return -EBUSY;
+               if (CDROM_CAN(CDC_LOCK))
+                       if ((ret=cdo->lock_door(cdi, 0)))
+                               return ret;
+
+               return cdo->tray_move(cdi, 1);
+               }
+
+       case CDROMCLOSETRAY: {
+               cdinfo(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n"); 
+               if (!CDROM_CAN(CDC_CLOSE_TRAY))
+                       return -ENOSYS;
+               return cdo->tray_move(cdi, 0);
+               }
+
+       case CDROMEJECT_SW: {
+               cdinfo(CD_DO_IOCTL, "entering CDROMEJECT_SW\n"); 
+               if (!CDROM_CAN(CDC_OPEN_TRAY))
+                       return -ENOSYS;
+               if (keeplocked)
+                       return -EBUSY;
+               cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
+               if (arg)
+                       cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
+               return 0;
+               }
+
+       case CDROM_MEDIA_CHANGED: {
+               struct cdrom_changer_info info;
+
+               cdinfo(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n"); 
+               if (!CDROM_CAN(CDC_MEDIA_CHANGED))
+                       return -ENOSYS;
+
+               /* cannot select disc or select current disc */
+               if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
+                       return media_changed(cdi, 1);
+
+               if ((unsigned int)arg >= cdi->capacity)
+                       return -EINVAL;
+
+               if ((ret = cdrom_read_mech_status(cdi, &info)))
+                       return ret;
+
+               return info.slots[arg].change;
+               }
+
+       case CDROM_SET_OPTIONS: {
+               cdinfo(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n"); 
+               /* options need to be in sync with capability. too late for
+                  that, so we have to check each one separately... */
+               switch (arg) {
+               case CDO_USE_FFLAGS:
+               case CDO_CHECK_TYPE:
+                       break;
+               case CDO_LOCK:
+                       if (!CDROM_CAN(CDC_LOCK))
+                               return -ENOSYS;
+                       break;
+               case 0:
+                       return cdi->options;
+               /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
+               default:
+                       if (!CDROM_CAN(arg))
+                               return -ENOSYS;
+               }
+               cdi->options |= (int) arg;
+               return cdi->options;
+               }
+
+       case CDROM_CLEAR_OPTIONS: {
+               cdinfo(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n"); 
+               cdi->options &= ~(int) arg;
+               return cdi->options;
+               }
+
+       case CDROM_SELECT_SPEED: {
+               cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n"); 
+               if (!CDROM_CAN(CDC_SELECT_SPEED))
+                       return -ENOSYS;
+               return cdo->select_speed(cdi, arg);
+               }
+
+       case CDROM_SELECT_DISC: {
+               cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n"); 
+               if (!CDROM_CAN(CDC_SELECT_DISC))
+                       return -ENOSYS;
+
+                if ((arg != CDSL_CURRENT) && (arg != CDSL_NONE))
+                       if ((int)arg >= cdi->capacity)
+                               return -EINVAL;
+
+               /* cdo->select_disc is a hook to allow a driver-specific
+                * way of seleting disc.  However, since there is no
+                * equiv hook for cdrom_slot_status this may not 
+                * actually be useful...
+                */
+               if (cdo->select_disc != NULL)
+                       return cdo->select_disc(cdi, arg);
+
+               /* no driver specific select_disc(), call our own */
+               cdinfo(CD_CHANGER, "Using generic cdrom_select_disc()\n"); 
+               return cdrom_select_disc(cdi, arg);
+               }
+
+       case CDROMRESET: {
+               if (!capable(CAP_SYS_ADMIN))
+                       return -EACCES;
+               cdinfo(CD_DO_IOCTL, "entering CDROM_RESET\n");
+               if (!CDROM_CAN(CDC_RESET))
+                       return -ENOSYS;
+               return cdo->reset(cdi);
+               }
+
+       case CDROM_LOCKDOOR: {
+               cdinfo(CD_DO_IOCTL, "%socking door.\n", arg ? "L" : "Unl");
+               if (!CDROM_CAN(CDC_LOCK))
+                       return -EDRIVE_CANT_DO_THIS;
+               keeplocked = arg ? 1 : 0;
+               /* don't unlock the door on multiple opens,but allow root
+                * to do so */
+               if ((cdi->use_count != 1) && !arg && !capable(CAP_SYS_ADMIN))
+                       return -EBUSY;
+               return cdo->lock_door(cdi, arg);
+               }
+
+       case CDROM_DEBUG: {
+               if (!capable(CAP_SYS_ADMIN))
+                       return -EACCES;
+               cdinfo(CD_DO_IOCTL, "%sabling debug.\n", arg ? "En" : "Dis");
+               debug = arg ? 1 : 0;
+               return debug;
+               }
+
+       case CDROM_GET_CAPABILITY: {
+               cdinfo(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
+               return (cdo->capability & ~cdi->mask);
+               }
+
+/* The following function is implemented, although very few audio
+ * discs give Universal Product Code information, which should just be
+ * the Medium Catalog Number on the box.  Note, that the way the code
+ * is written on the CD is /not/ uniform across all discs!
+ */
+       case CDROM_GET_MCN: {
+               struct cdrom_mcn mcn;
+               cdinfo(CD_DO_IOCTL, "entering CDROM_GET_MCN\n"); 
+               if (!(cdo->capability & CDC_MCN))
+                       return -ENOSYS;
+               if ((ret=cdo->get_mcn(cdi, &mcn)))
+                       return ret;
+               IOCTL_OUT(arg, struct cdrom_mcn, mcn);
+               cdinfo(CD_DO_IOCTL, "CDROM_GET_MCN successful\n"); 
+               return 0;
+               }
+
+       case CDROM_DRIVE_STATUS: {
+               cdinfo(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n"); 
+               if (!(cdo->capability & CDC_DRIVE_STATUS))
+                       return -ENOSYS;
+               if (!CDROM_CAN(CDC_SELECT_DISC))
+                       return cdo->drive_status(cdi, CDSL_CURRENT);
+                if ((arg == CDSL_CURRENT) || (arg == CDSL_NONE)) 
+                       return cdo->drive_status(cdi, CDSL_CURRENT);
+               if (((int)arg >= cdi->capacity))
+                       return -EINVAL;
+               return cdrom_slot_status(cdi, arg);
+               }
+
+       /* Ok, this is where problems start.  The current interface for the
+          CDROM_DISC_STATUS ioctl is flawed.  It makes the false assumption
+          that CDs are all CDS_DATA_1 or all CDS_AUDIO, etc.  Unfortunatly,
+          while this is often the case, it is also very common for CDs to
+          have some tracks with data, and some tracks with audio.  Just 
+          because I feel like it, I declare the following to be the best
+          way to cope.  If the CD has ANY data tracks on it, it will be
+          returned as a data CD.  If it has any XA tracks, I will return
+          it as that.  Now I could simplify this interface by combining these 
+          returns with the above, but this more clearly demonstrates
+          the problem with the current interface.  Too bad this wasn't 
+          designed to use bitmasks...         -Erik 
+
+          Well, now we have the option CDS_MIXED: a mixed-type CD. 
+          User level programmers might feel the ioctl is not very useful.
+                                               ---david
+       */
+       case CDROM_DISC_STATUS: {
+               tracktype tracks;
+               cdinfo(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n"); 
+               cdrom_count_tracks(cdi, &tracks);
+               if (tracks.error) 
+                       return(tracks.error);
+
+               /* Policy mode on */
+               if (tracks.audio > 0) {
+                       if (tracks.data==0 && tracks.cdi==0 && tracks.xa==0) 
+                               return CDS_AUDIO;
+                       else
+                               return CDS_MIXED;
+               }
+               if (tracks.cdi > 0) return CDS_XA_2_2;
+               if (tracks.xa > 0) return CDS_XA_2_1;
+               if (tracks.data > 0) return CDS_DATA_1;
+               /* Policy mode off */
+
+               cdinfo(CD_WARNING,"This disc doesn't have any tracks I recognize!\n");
+               return CDS_NO_INFO;
+               }
+
+       case CDROM_CHANGER_NSLOTS: {
+               cdinfo(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n"); 
+               return cdi->capacity;
+               }
+       }
+
+       /* use the ioctls that are implemented through the generic_packet()
+          interface. this may look at bit funny, but if -ENOTTY is
+          returned that particular ioctl is not implemented and we
+          let it go through the device specific ones. */
+       if (CDROM_CAN(CDC_GENERIC_PACKET)) {
+               ret = mmc_ioctl(cdi, cmd, arg);
+               if (ret != -ENOTTY) {
+                       return ret;
+               }
+       }
+
+       /* note: most of the cdinfo() calls are commented out here,
+          because they fill up the sys log when CD players poll
+          the drive. */
+       switch (cmd) {
+       case CDROMSUBCHNL: {
+               struct cdrom_subchnl q;
+               u_char requested, back;
+               if (!CDROM_CAN(CDC_PLAY_AUDIO))
+                       return -ENOSYS;
+               /* cdinfo(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/ 
+               IOCTL_IN(arg, struct cdrom_subchnl, q);
+               requested = q.cdsc_format;
+               if (!((requested == CDROM_MSF) ||
+                     (requested == CDROM_LBA)))
+                       return -EINVAL;
+               q.cdsc_format = CDROM_MSF;
+               if ((ret=cdo->audio_ioctl(cdi, cmd, &q)))
+                       return ret;
+               back = q.cdsc_format; /* local copy */
+               sanitize_format(&q.cdsc_absaddr, &back, requested);
+               sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
+               IOCTL_OUT(arg, struct cdrom_subchnl, q);
+               /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */ 
+               return 0;
+               }
+       case CDROMREADTOCHDR: {
+               struct cdrom_tochdr header;
+               if (!CDROM_CAN(CDC_PLAY_AUDIO))
+                       return -ENOSYS;
+               /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */ 
+               IOCTL_IN(arg, struct cdrom_tochdr, header);
+               if ((ret=cdo->audio_ioctl(cdi, cmd, &header)))
+                       return ret;
+               IOCTL_OUT(arg, struct cdrom_tochdr, header);
+               /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */ 
+               return 0;
+               }
+       case CDROMREADTOCENTRY: {
+               struct cdrom_tocentry entry;
+               u_char requested_format;
+               if (!CDROM_CAN(CDC_PLAY_AUDIO))
+                       return -ENOSYS;
+               /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */ 
+               IOCTL_IN(arg, struct cdrom_tocentry, entry);
+               requested_format = entry.cdte_format;
+               if (!((requested_format == CDROM_MSF) || 
+                       (requested_format == CDROM_LBA)))
+                               return -EINVAL;
+               /* make interface to low-level uniform */
+               entry.cdte_format = CDROM_MSF;
+               if ((ret=cdo->audio_ioctl(cdi, cmd, &entry)))
+                       return ret;
+               sanitize_format(&entry.cdte_addr,
+               &entry.cdte_format, requested_format);
+               IOCTL_OUT(arg, struct cdrom_tocentry, entry);
+               /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */ 
+               return 0;
+               }
+       case CDROMPLAYMSF: {
+               struct cdrom_msf msf;
+               if (!CDROM_CAN(CDC_PLAY_AUDIO))
+                       return -ENOSYS;
+               cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n"); 
+               IOCTL_IN(arg, struct cdrom_msf, msf);
+               return cdo->audio_ioctl(cdi, cmd, &msf);
+               }
+       case CDROMPLAYTRKIND: {
+               struct cdrom_ti ti;
+               if (!CDROM_CAN(CDC_PLAY_AUDIO))
+                       return -ENOSYS;
+               cdinfo(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n"); 
+               IOCTL_IN(arg, struct cdrom_ti, ti);
+               CHECKAUDIO;
+               return cdo->audio_ioctl(cdi, cmd, &ti);
+               }
+       case CDROMVOLCTRL: {
+               struct cdrom_volctrl volume;
+               if (!CDROM_CAN(CDC_PLAY_AUDIO))
+                       return -ENOSYS;
+               cdinfo(CD_DO_IOCTL, "entering CDROMVOLCTRL\n"); 
+               IOCTL_IN(arg, struct cdrom_volctrl, volume);
+               return cdo->audio_ioctl(cdi, cmd, &volume);
+               }
+       case CDROMVOLREAD: {
+               struct cdrom_volctrl volume;
+               if (!CDROM_CAN(CDC_PLAY_AUDIO))
+                       return -ENOSYS;
+               cdinfo(CD_DO_IOCTL, "entering CDROMVOLREAD\n"); 
+               if ((ret=cdo->audio_ioctl(cdi, cmd, &volume)))
+                       return ret;
+               IOCTL_OUT(arg, struct cdrom_volctrl, volume);
+               return 0;
+               }
+       case CDROMSTART:
+       case CDROMSTOP:
+       case CDROMPAUSE:
+       case CDROMRESUME: {
+               if (!CDROM_CAN(CDC_PLAY_AUDIO))
+                       return -ENOSYS;
+               cdinfo(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n"); 
+               CHECKAUDIO;
+               return cdo->audio_ioctl(cdi, cmd, NULL);
+               }
+       } /* switch */
+
+       /* do the device specific ioctls */
+       if (CDROM_CAN(CDC_IOCTLS))
+               return cdo->dev_ioctl(cdi, cmd, arg);
+       
+       return -ENOSYS;
+}
+
+static inline
+int msf_to_lba(char m, char s, char f)
+{
+       return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
+}
+
+/*
+ * Required when we need to use READ_10 to issue other than 2048 block
+ * reads
+ */
+static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
+{
+       struct cdrom_device_ops *cdo = cdi->ops;
+       struct cdrom_generic_command cgc;
+       struct modesel_head mh;
+
+       memset(&mh, 0, sizeof(mh));
+       mh.block_desc_length = 0x08;
+       mh.block_length_med = (size >> 8) & 0xff;
+       mh.block_length_lo = size & 0xff;
+
+       memset(&cgc, 0, sizeof(cgc));
+       cgc.cmd[0] = 0x15;
+       cgc.cmd[1] = 1 << 4;
+       cgc.cmd[4] = 12;
+       cgc.buflen = sizeof(mh);
+       cgc.buffer = (char *) &mh;
+       cgc.data_direction = CGC_DATA_WRITE;
+       mh.block_desc_length = 0x08;
+       mh.block_length_med = (size >> 8) & 0xff;
+       mh.block_length_lo = size & 0xff;
+
+       return cdo->generic_packet(cdi, &cgc);
+}
+
+static int cdrom_do_cmd(struct cdrom_device_info *cdi,
+                       struct cdrom_generic_command *cgc)
+{
+       struct request_sense *usense, sense;
+       unsigned char *ubuf;
+       int ret;
+
+       if (cgc->data_direction == CGC_DATA_UNKNOWN)
+               return -EINVAL;
+
+       if (cgc->buflen < 0 || cgc->buflen >= 131072)
+               return -EINVAL;
+
+       if ((ubuf = cgc->buffer)) {
+               cgc->buffer = kmalloc(cgc->buflen, GFP_KERNEL);
+               if (cgc->buffer == NULL)
+                       return -ENOMEM;
+       }
+
+       usense = cgc->sense;
+       cgc->sense = &sense;
+       if (usense && !access_ok(VERIFY_WRITE, usense, sizeof(*usense)))
+               return -EFAULT;
+
+       if (cgc->data_direction == CGC_DATA_READ) {
+               if (!access_ok(VERIFY_READ, ubuf, cgc->buflen))
+                       return -EFAULT;
+       } else if (cgc->data_direction == CGC_DATA_WRITE) {
+               if (copy_from_user(cgc->buffer, ubuf, cgc->buflen)) {
+                       kfree(cgc->buffer);
+                       return -EFAULT;
+               }
+       }
+
+       ret = cdi->ops->generic_packet(cdi, cgc);
+       __copy_to_user(usense, cgc->sense, sizeof(*usense));
+       if (!ret && cgc->data_direction == CGC_DATA_READ)
+               __copy_to_user(ubuf, cgc->buffer, cgc->buflen);
+       kfree(cgc->buffer);
+       return ret;
+}
+
+static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
+                    unsigned long arg)
+{              
+       struct cdrom_device_ops *cdo = cdi->ops;
+       struct cdrom_generic_command cgc;
+       kdev_t dev = cdi->dev;
+       char buffer[32];
+       int ret = 0;
+
+       memset(&cgc, 0, sizeof(cgc));
+
+       /* build a unified command and queue it through
+          cdo->generic_packet() */
+       switch (cmd) {
+       case CDROMREADRAW:
+       case CDROMREADMODE1:
+       case CDROMREADMODE2: {
+               struct cdrom_msf msf;
+               int blocksize = 0, format = 0, lba;
+               
+               switch (cmd) {
+               case CDROMREADRAW:
+                       blocksize = CD_FRAMESIZE_RAW;
+                       break;
+               case CDROMREADMODE1:
+                       blocksize = CD_FRAMESIZE;
+                       format = 2;
+                       break;
+               case CDROMREADMODE2:
+                       blocksize = CD_FRAMESIZE_RAW0;
+                       break;
+               }
+               IOCTL_IN(arg, struct cdrom_msf, msf);
+               lba = msf_to_lba(msf.cdmsf_min0,msf.cdmsf_sec0,msf.cdmsf_frame0);
+               /* FIXME: we need upper bound checking, too!! */
+               if (lba < 0)
+                       return -EINVAL;
+               cgc.buffer = (char *) kmalloc(blocksize, GFP_KERNEL);
+               if (cgc.buffer == NULL)
+                       return -ENOMEM;
+               cgc.data_direction = CGC_DATA_READ;
+               ret = cdrom_read_block(cdi, &cgc, lba, 1, format, blocksize);
+               if (ret) {
+                       /*
+                        * SCSI-II devices are not required to support
+                        * READ_CD, so let's try switching block size
+                        */
+                       /* FIXME: switch back again... */
+                       if ((ret = cdrom_switch_blocksize(cdi, blocksize))) {
+                               kfree(cgc.buffer);
+                               return ret;
+                       }
+                       cgc.sense = NULL;
+                       ret = cdrom_read_cd(cdi, &cgc, lba, blocksize, 1);
+                       ret |= cdrom_switch_blocksize(cdi, blocksize);
+               }
+               if (!ret && copy_to_user((char *)arg, cgc.buffer, blocksize))
+                       ret = -EFAULT;
+               kfree(cgc.buffer);
+               return ret;
+               }
+       case CDROMREADAUDIO: {
+               struct cdrom_read_audio ra;
+               int lba, nr;
+
+               IOCTL_IN(arg, struct cdrom_read_audio, ra);
+
+               if (ra.addr_format == CDROM_MSF)
+                       lba = msf_to_lba(ra.addr.msf.minute,
+                                        ra.addr.msf.second,
+                                        ra.addr.msf.frame);
+               else if (ra.addr_format == CDROM_LBA)
+                       lba = ra.addr.lba;
+               else
+                       return -EINVAL;
+
+               /* FIXME: we need upper bound checking, too!! */
+               if (lba < 0 || ra.nframes <= 0)
+                       return -EINVAL;
+
+               /*
+                * start with will ra.nframes size, back down if alloc fails
+                */
+               nr = ra.nframes;
+               do {
+                       cgc.buffer = kmalloc(CD_FRAMESIZE_RAW * nr, GFP_KERNEL);
+                       if (cgc.buffer)
+                               break;
+
+                       nr >>= 1;
+               } while (nr);
+
+               if (!nr)
+                       return -ENOMEM;
+
+               if (!access_ok(VERIFY_WRITE, ra.buf, ra.nframes*CD_FRAMESIZE_RAW)) {
+                       kfree(cgc.buffer);
+                       return -EFAULT;
+               }
+               cgc.data_direction = CGC_DATA_READ;
+               while (ra.nframes > 0) {
+                       if (nr > ra.nframes)
+                               nr = ra.nframes;
+
+                       ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
+                       if (ret)
+                               break;
+                       __copy_to_user(ra.buf, cgc.buffer, CD_FRAMESIZE_RAW*nr);
+                       ra.buf += CD_FRAMESIZE_RAW * nr;
+                       ra.nframes -= nr;
+                       lba += nr;
+               }
+               kfree(cgc.buffer);
+               return ret;
+               }
+       case CDROMSUBCHNL: {
+               struct cdrom_subchnl q;
+               u_char requested, back;
+               IOCTL_IN(arg, struct cdrom_subchnl, q);
+               requested = q.cdsc_format;
+               if (!((requested == CDROM_MSF) ||
+                     (requested == CDROM_LBA)))
+                       return -EINVAL;
+               q.cdsc_format = CDROM_MSF;
+               if ((ret = cdrom_read_subchannel(cdi, &q, 0)))
+                       return ret;
+               back = q.cdsc_format; /* local copy */
+               sanitize_format(&q.cdsc_absaddr, &back, requested);
+               sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
+               IOCTL_OUT(arg, struct cdrom_subchnl, q);
+               /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */ 
+               return 0;
+               }
+       case CDROMPLAYMSF: {
+               struct cdrom_msf msf;
+               cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
+               IOCTL_IN(arg, struct cdrom_msf, msf);
+               cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
+               cgc.cmd[3] = msf.cdmsf_min0;
+               cgc.cmd[4] = msf.cdmsf_sec0;
+               cgc.cmd[5] = msf.cdmsf_frame0;
+               cgc.cmd[6] = msf.cdmsf_min1;
+               cgc.cmd[7] = msf.cdmsf_sec1;
+               cgc.cmd[8] = msf.cdmsf_frame1;
+               cgc.data_direction = CGC_DATA_NONE;
+               return cdo->generic_packet(cdi, &cgc);
+               }
+       case CDROMPLAYBLK: {
+               struct cdrom_blk blk;
+               cdinfo(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
+               IOCTL_IN(arg, struct cdrom_blk, blk);
+               cgc.cmd[0] = GPCMD_PLAY_AUDIO_10;
+               cgc.cmd[2] = (blk.from >> 24) & 0xff;
+               cgc.cmd[3] = (blk.from >> 16) & 0xff;
+               cgc.cmd[4] = (blk.from >>  8) & 0xff;
+               cgc.cmd[5] = blk.from & 0xff;
+               cgc.cmd[7] = (blk.len >> 8) & 0xff;
+               cgc.cmd[8] = blk.len & 0xff;
+               cgc.data_direction = CGC_DATA_NONE;
+               return cdo->generic_packet(cdi, &cgc);
+               }
+       case CDROMVOLCTRL:
+       case CDROMVOLREAD: {
+               struct cdrom_volctrl volctrl;
+               char mask[32];
+               unsigned short offset;
+               cdinfo(CD_DO_IOCTL, "entering CDROMVOLUME\n");
+
+               IOCTL_IN(arg, struct cdrom_volctrl, volctrl);
+
+               cgc.buffer = buffer;
+               cgc.buflen = 24;
+               if ((ret = cdrom_mode_sense(cdi, &cgc, GPMODE_AUDIO_CTL_PAGE, 0)))
+                   return ret;
+               
+               /* some drives have longer pages, adjust and reread. */
+               if (buffer[1] > cgc.buflen) {
+                       cgc.buflen = buffer[1] + 2;
+                       if ((ret = cdrom_mode_sense(cdi, &cgc, 
+                                       GPMODE_AUDIO_CTL_PAGE, 0))) 
+                           return ret;
+               }
+               
+               /* get the offset from the length of the page. length
+                  is measure from byte 2 an on, thus the 14. */
+               offset = buffer[1] - 14;
+
+               /* now we have the current volume settings. if it was only
+                  a CDROMVOLREAD, return these values */
+               if (cmd == CDROMVOLREAD) {
+                       volctrl.channel0 = buffer[offset+9];
+                       volctrl.channel1 = buffer[offset+11];
+                       volctrl.channel2 = buffer[offset+13];
+                       volctrl.channel3 = buffer[offset+15];
+                       IOCTL_OUT(arg, struct cdrom_volctrl, volctrl);
+                       return 0;
+               }
+               
+               /* get the volume mask */
+               cgc.buffer = mask;
+               if ((ret = cdrom_mode_sense(cdi, &cgc, 
+                               GPMODE_AUDIO_CTL_PAGE, 1)))
+                       return ret;
+
+               buffer[offset+9] = volctrl.channel0 & mask[offset+9];
+               buffer[offset+11] = volctrl.channel1 & mask[offset+11];
+               buffer[offset+13] = volctrl.channel2 & mask[offset+13];
+               buffer[offset+15] = volctrl.channel3 & mask[offset+15];
+
+               /* set volume */
+               cgc.buffer = buffer;
+               return cdrom_mode_select(cdi, &cgc);
+               }
+
+       case CDROMSTART:
+       case CDROMSTOP: {
+               cdinfo(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n"); 
+               cgc.cmd[0] = GPCMD_START_STOP_UNIT;
+               cgc.cmd[1] = 1;
+               cgc.cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
+               cgc.data_direction = CGC_DATA_NONE;
+               return cdo->generic_packet(cdi, &cgc);
+               }
+
+       case CDROMPAUSE:
+       case CDROMRESUME: {
+               cdinfo(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n"); 
+               cgc.cmd[0] = GPCMD_PAUSE_RESUME;
+               cgc.cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
+               cgc.data_direction = CGC_DATA_NONE;
+               return cdo->generic_packet(cdi, &cgc);
+               }
+
+       case DVD_READ_STRUCT: {
+               dvd_struct *s;
+               int size = sizeof(dvd_struct);
+               if (!CDROM_CAN(CDC_DVD))
+                       return -ENOSYS;
+               if ((s = (dvd_struct *) kmalloc(size, GFP_KERNEL)) == NULL)
+                       return -ENOMEM;
+               cdinfo(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n"); 
+               if (copy_from_user(s, (dvd_struct *)arg, size)) {
+                       kfree(s);
+                       return -EFAULT;
+               }
+               if ((ret = dvd_read_struct(cdi, s))) {
+                       kfree(s);
+                       return ret;
+               }
+               if (copy_to_user((dvd_struct *)arg, s, size))
+                       ret = -EFAULT;
+               kfree(s);
+               return ret;
+               }
+
+       case DVD_AUTH: {
+               dvd_authinfo ai;
+               if (!CDROM_CAN(CDC_DVD))
+                       return -ENOSYS;
+               cdinfo(CD_DO_IOCTL, "entering DVD_AUTH\n"); 
+               IOCTL_IN(arg, dvd_authinfo, ai);
+               if ((ret = dvd_do_auth (cdi, &ai)))
+                       return ret;
+               IOCTL_OUT(arg, dvd_authinfo, ai);
+               return 0;
+               }
+
+       case CDROM_SEND_PACKET: {
+               if (!CDROM_CAN(CDC_GENERIC_PACKET))
+                       return -ENOSYS;
+               cdinfo(CD_DO_IOCTL, "entering CDROM_SEND_PACKET\n"); 
+               IOCTL_IN(arg, struct cdrom_generic_command, cgc);
+               return cdrom_do_cmd(cdi, &cgc);
+               }
+       case CDROM_NEXT_WRITABLE: {
+               long next = 0;
+               cdinfo(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n"); 
+               if ((ret = cdrom_get_next_writable(dev, &next)))
+                       return ret;
+               IOCTL_OUT(arg, long, next);
+               return 0;
+               }
+       case CDROM_LAST_WRITTEN: {
+               long last = 0;
+               cdinfo(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n"); 
+               if ((ret = cdrom_get_last_written(dev, &last)))
+                       return ret;
+               IOCTL_OUT(arg, long, last);
+               return 0;
+               }
+       } /* switch */
+
+       return -ENOTTY;
+}
+
+int cdrom_get_track_info(kdev_t dev, __u16 track, __u8 type,
+                        track_information *ti)
+{
+        struct cdrom_device_info *cdi = cdrom_find_device(dev);
+       struct cdrom_device_ops *cdo = cdi->ops;
+       struct cdrom_generic_command cgc;
+       int ret;
+
+       init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
+       cgc.cmd[1] = type & 3;
+       cgc.cmd[4] = (track & 0xff00) >> 8;
+       cgc.cmd[5] = track & 0xff;
+       cgc.cmd[8] = 8;
+       cgc.quiet = 1;
+
+       if ((ret = cdo->generic_packet(cdi, &cgc)))
+               return ret;
+       
+       cgc.buflen = be16_to_cpu(ti->track_information_length) +
+                    sizeof(ti->track_information_length);
+
+       if (cgc.buflen > sizeof(track_information))
+               cgc.buflen = sizeof(track_information);
+
+       cgc.cmd[8] = cgc.buflen;
+       return cdo->generic_packet(cdi, &cgc);
+}
+
+/* requires CD R/RW */
+int cdrom_get_disc_info(kdev_t dev, disc_information *di)
+{
+       struct cdrom_device_info *cdi = cdrom_find_device(dev);
+       struct cdrom_device_ops *cdo = cdi->ops;
+       struct cdrom_generic_command cgc;
+       int ret;
+
+       /* set up command and get the disc info */
+       init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
+       cgc.cmd[0] = GPCMD_READ_DISC_INFO;
+       cgc.cmd[8] = cgc.buflen = 2;
+       cgc.quiet = 1;
+
+       if ((ret = cdo->generic_packet(cdi, &cgc)))
+               return ret;
+
+       /* not all drives have the same disc_info length, so requeue
+        * packet with the length the drive tells us it can supply
+        */
+       cgc.buflen = be16_to_cpu(di->disc_information_length) +
+                    sizeof(di->disc_information_length);
+
+       if (cgc.buflen > sizeof(disc_information))
+               cgc.buflen = sizeof(disc_information);
+
+       cgc.cmd[8] = cgc.buflen;
+       return cdo->generic_packet(cdi, &cgc);
+}
+
+
+/* return the last written block on the CD-R media. this is for the udf
+   file system. */
+int cdrom_get_last_written(kdev_t dev, long *last_written)
+{      
+       struct cdrom_device_info *cdi = cdrom_find_device(dev);
+       struct cdrom_tocentry toc;
+       disc_information di;
+       track_information ti;
+       __u32 last_track;
+       int ret = -1;
+
+       if (!CDROM_CAN(CDC_GENERIC_PACKET))
+               goto use_toc;
+
+       if ((ret = cdrom_get_disc_info(dev, &di)))
+               goto use_toc;
+
+       last_track = (di.last_track_msb << 8) | di.last_track_lsb;
+       if ((ret = cdrom_get_track_info(dev, last_track, 1, &ti)))
+               goto use_toc;
+
+       /* if this track is blank, try the previous. */
+       if (ti.blank) {
+               last_track--;
+               if ((ret = cdrom_get_track_info(dev, last_track, 1, &ti)))
+                       goto use_toc;
+       }
+
+       /* if last recorded field is valid, return it. */
+       if (ti.lra_v) {
+               *last_written = be32_to_cpu(ti.last_rec_address);
+       } else {
+               /* make it up instead */
+               *last_written = be32_to_cpu(ti.track_start) +
+                               be32_to_cpu(ti.track_size);
+               if (ti.free_blocks)
+                       *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
+       }
+       return 0;
+
+       /* this is where we end up if the drive either can't do a
+          GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
+          it fails. then we return the toc contents. */
+use_toc:
+       toc.cdte_format = CDROM_MSF;
+       toc.cdte_track = CDROM_LEADOUT;
+       if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc))
+               return ret;
+       sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
+       *last_written = toc.cdte_addr.lba;
+       return 0;
+}
+
+/* return the next writable block. also for udf file system. */
+int cdrom_get_next_writable(kdev_t dev, long *next_writable)
+{
+       struct cdrom_device_info *cdi = cdrom_find_device(dev);
+       disc_information di;
+       track_information ti;
+       __u16 last_track;
+       int ret = -1;
+
+       if (!CDROM_CAN(CDC_GENERIC_PACKET))
+               goto use_last_written;
+
+       if ((ret = cdrom_get_disc_info(dev, &di)))
+               goto use_last_written;
+
+       last_track = (di.last_track_msb << 8) | di.last_track_lsb;
+       if ((ret = cdrom_get_track_info(dev, last_track, 1, &ti)))
+               goto use_last_written;
+
+        /* if this track is blank, try the previous. */
+       if (ti.blank) {
+               last_track--;
+               if ((ret = cdrom_get_track_info(dev, last_track, 1, &ti)))
+                       goto use_last_written;
+       }
+
+       /* if next recordable address field is valid, use it. */
+       if (ti.nwa_v)
+               *next_writable = be32_to_cpu(ti.next_writable);
+       else
+               goto use_last_written;
+
+       return 0;
+
+use_last_written:
+       if ((ret = cdrom_get_last_written(dev, next_writable))) {
+               *next_writable = 0;
+               return ret;
+       } else {
+               *next_writable += 7;
+               return 0;
+       }
+}
+
+EXPORT_SYMBOL(cdrom_get_disc_info);
+EXPORT_SYMBOL(cdrom_get_track_info);
+EXPORT_SYMBOL(cdrom_get_next_writable);
+EXPORT_SYMBOL(cdrom_get_last_written);
+EXPORT_SYMBOL(cdrom_count_tracks);
+EXPORT_SYMBOL(register_cdrom);
+EXPORT_SYMBOL(unregister_cdrom);
+EXPORT_SYMBOL(cdrom_open);
+EXPORT_SYMBOL(cdrom_release);
+EXPORT_SYMBOL(cdrom_ioctl);
+EXPORT_SYMBOL(cdrom_media_changed);
+EXPORT_SYMBOL(cdrom_number_of_slots);
+EXPORT_SYMBOL(cdrom_select_disc);
+EXPORT_SYMBOL(cdrom_mode_select);
+EXPORT_SYMBOL(cdrom_mode_sense);
+EXPORT_SYMBOL(init_cdrom_command);
+EXPORT_SYMBOL(cdrom_find_device);
+
+#ifdef CONFIG_SYSCTL
+
+#define CDROM_STR_SIZE 1000
+
+struct cdrom_sysctl_settings {
+       char    info[CDROM_STR_SIZE];   /* general info */
+       int     autoclose;              /* close tray upon mount, etc */
+       int     autoeject;              /* eject on umount */
+       int     debug;                  /* turn on debugging messages */
+       int     lock;                   /* lock the door on device open */
+       int     check;                  /* check media type */
+} cdrom_sysctl_settings;
+
+int cdrom_sysctl_info(ctl_table *ctl, int write, struct file * filp,
+                           void *buffer, size_t *lenp)
+{
+        int pos;
+       struct cdrom_device_info *cdi;
+       char *info = cdrom_sysctl_settings.info;
+       
+       if (!*lenp || (filp->f_pos && !write)) {
+               *lenp = 0;
+               return 0;
+       }
+
+       pos = sprintf(info, "CD-ROM information, " VERSION "\n");
+       
+       pos += sprintf(info+pos, "\ndrive name:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%s", cdi->name);
+
+       pos += sprintf(info+pos, "\ndrive speed:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", cdi->speed);
+
+       pos += sprintf(info+pos, "\ndrive # of slots:");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", cdi->capacity);
+
+       pos += sprintf(info+pos, "\nCan close tray:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CLOSE_TRAY) != 0);
+
+       pos += sprintf(info+pos, "\nCan open tray:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_OPEN_TRAY) != 0);
+
+       pos += sprintf(info+pos, "\nCan lock tray:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_LOCK) != 0);
+
+       pos += sprintf(info+pos, "\nCan change speed:");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_SPEED) != 0);
+
+       pos += sprintf(info+pos, "\nCan select disk:");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_DISC) != 0);
+
+       pos += sprintf(info+pos, "\nCan read multisession:");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MULTI_SESSION) != 0);
+
+       pos += sprintf(info+pos, "\nCan read MCN:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MCN) != 0);
+
+       pos += sprintf(info+pos, "\nReports media changed:");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MEDIA_CHANGED) != 0);
+
+       pos += sprintf(info+pos, "\nCan play audio:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_PLAY_AUDIO) != 0);
+
+       pos += sprintf(info+pos, "\nCan write CD-R:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_R) != 0);
+
+       pos += sprintf(info+pos, "\nCan write CD-RW:");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_RW) != 0);
+
+       pos += sprintf(info+pos, "\nCan read DVD:\t");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD) != 0);
+
+       pos += sprintf(info+pos, "\nCan write DVD-R:");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_R) != 0);
+
+       pos += sprintf(info+pos, "\nCan write DVD-RAM:");
+       for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
+           pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_RAM) != 0);
+
+       strcpy(info+pos,"\n\n");
+               
+        return proc_dostring(ctl, write, filp, buffer, lenp);
+}
+
+/* Unfortunately, per device settings are not implemented through
+   procfs/sysctl yet. When they are, this will naturally disappear. For now
+   just update all drives. Later this will become the template on which
+   new registered drives will be based. */
+void cdrom_update_settings(void)
+{
+       struct cdrom_device_info *cdi;
+
+       for (cdi = topCdromPtr; cdi != NULL; cdi = cdi->next) {
+               if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
+                       cdi->options |= CDO_AUTO_CLOSE;
+               else if (!autoclose)
+                       cdi->options &= ~CDO_AUTO_CLOSE;
+               if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
+                       cdi->options |= CDO_AUTO_EJECT;
+               else if (!autoeject)
+                       cdi->options &= ~CDO_AUTO_EJECT;
+               if (lockdoor && CDROM_CAN(CDC_LOCK))
+                       cdi->options |= CDO_LOCK;
+               else if (!lockdoor)
+                       cdi->options &= ~CDO_LOCK;
+               if (check_media_type)
+                       cdi->options |= CDO_CHECK_TYPE;
+               else
+                       cdi->options &= ~CDO_CHECK_TYPE;
+       }
+}
+
+static int cdrom_sysctl_handler(ctl_table *ctl, int write, struct file * filp,
+                               void *buffer, size_t *lenp)
+{
+       int *valp = ctl->data;
+       int val = *valp;
+       int ret;
+       
+       ret = proc_dointvec(ctl, write, filp, buffer, lenp);
+
+       if (write && *valp != val) {
+       
+               /* we only care for 1 or 0. */
+               if (*valp)
+                       *valp = 1;
+               else
+                       *valp = 0;
+
+               switch (ctl->ctl_name) {
+               case DEV_CDROM_AUTOCLOSE: {
+                       if (valp == &cdrom_sysctl_settings.autoclose)
+                               autoclose = cdrom_sysctl_settings.autoclose;
+                       break;
+                       }
+               case DEV_CDROM_AUTOEJECT: {
+                       if (valp == &cdrom_sysctl_settings.autoeject)
+                               autoeject = cdrom_sysctl_settings.autoeject;
+                       break;
+                       }
+               case DEV_CDROM_DEBUG: {
+                       if (valp == &cdrom_sysctl_settings.debug)
+                               debug = cdrom_sysctl_settings.debug;
+                       break;
+                       }
+               case DEV_CDROM_LOCK: {
+                       if (valp == &cdrom_sysctl_settings.lock)
+                               lockdoor = cdrom_sysctl_settings.lock;
+                       break;
+                       }
+               case DEV_CDROM_CHECK_MEDIA: {
+                       if (valp == &cdrom_sysctl_settings.check)
+                               check_media_type = cdrom_sysctl_settings.check;
+                       break;
+                       }
+               }
+               /* update the option flags according to the changes. we
+                  don't have per device options through sysctl yet,
+                  but we will have and then this will disappear. */
+               cdrom_update_settings();
+       }
+
+        return ret;
+}
+
+/* Place files in /proc/sys/dev/cdrom */
+ctl_table cdrom_table[] = {
+       {DEV_CDROM_INFO, "info", &cdrom_sysctl_settings.info, 
+               CDROM_STR_SIZE, 0444, NULL, &cdrom_sysctl_info},
+       {DEV_CDROM_AUTOCLOSE, "autoclose", &cdrom_sysctl_settings.autoclose,
+               sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
+       {DEV_CDROM_AUTOEJECT, "autoeject", &cdrom_sysctl_settings.autoeject,
+               sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
+       {DEV_CDROM_DEBUG, "debug", &cdrom_sysctl_settings.debug,
+               sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
+       {DEV_CDROM_LOCK, "lock", &cdrom_sysctl_settings.lock,
+               sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
+       {DEV_CDROM_CHECK_MEDIA, "check_media", &cdrom_sysctl_settings.check,
+               sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
+       {0}
+       };
+
+ctl_table cdrom_cdrom_table[] = {
+       {DEV_CDROM, "cdrom", NULL, 0, 0555, cdrom_table},
+       {0}
+       };
+
+/* Make sure that /proc/sys/dev is there */
+ctl_table cdrom_root_table[] = {
+#ifdef CONFIG_PROC_FS
+       {CTL_DEV, "dev", NULL, 0, 0555, cdrom_cdrom_table},
+#endif /* CONFIG_PROC_FS */
+       {0}
+       };
+static struct ctl_table_header *cdrom_sysctl_header;
+
+static void cdrom_sysctl_register(void)
+{
+       static int initialized;
+
+       if (initialized == 1)
+               return;
+
+       cdrom_sysctl_header = register_sysctl_table(cdrom_root_table, 1);
+       cdrom_root_table->child->de->owner = THIS_MODULE;
+
+       /* set the defaults */
+       cdrom_sysctl_settings.autoclose = autoclose;
+       cdrom_sysctl_settings.autoeject = autoeject;
+       cdrom_sysctl_settings.debug = debug;
+       cdrom_sysctl_settings.lock = lockdoor;
+       cdrom_sysctl_settings.check = check_media_type;
+
+       initialized = 1;
+}
+
+static void cdrom_sysctl_unregister(void)
+{
+       if (cdrom_sysctl_header)
+               unregister_sysctl_table(cdrom_sysctl_header);
+}
+
+#endif /* CONFIG_SYSCTL */
+
+static int __init cdrom_init(void)
+{
+#ifdef CONFIG_SYSCTL
+       cdrom_sysctl_register();
+#endif
+#if 0
+       if (!devfs_handle)
+               devfs_handle = devfs_mk_dir(NULL, "cdroms", NULL);
+#endif
+       return 0;
+}
+
+static void __exit cdrom_exit(void)
+{
+       printk(KERN_INFO "Uniform CD-ROM driver unloaded\n");
+#ifdef CONFIG_SYSCTL
+       cdrom_sysctl_unregister();
+#endif
+#if 0
+       devfs_unregister(devfs_handle);
+#endif
+}
+
+module_init(cdrom_init);
+module_exit(cdrom_exit);
+MODULE_LICENSE("GPL");
diff --git a/xen/drivers/ide/ide-cd.c b/xen/drivers/ide/ide-cd.c
new file mode 100644 (file)
index 0000000..c851f04
--- /dev/null
@@ -0,0 +1,3072 @@
+/*
+ * linux/drivers/ide/ide-cd.c
+ *
+ * Copyright (C) 1994, 1995, 1996  scott snyder  <snyder@fnald0.fnal.gov>
+ * Copyright (C) 1996-1998  Erik Andersen <andersee@debian.org>
+ * Copyright (C) 1998-2000  Jens Axboe <axboe@suse.de>
+ *
+ * May be copied or modified under the terms of the GNU General Public
+ * License.  See linux/COPYING for more information.
+ *
+ * ATAPI CD-ROM driver.  To be used with ide.c.
+ * See Documentation/cdrom/ide-cd for usage information.
+ *
+ * Suggestions are welcome. Patches that work are more welcome though. ;-)
+ * For those wishing to work on this driver, please be sure you download
+ * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI 
+ * (SFF-8020i rev 2.6) standards. These documents can be obtained by 
+ * anonymous ftp from:
+ * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
+ * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
+ *
+ * Drives that deviate from these standards will be accomodated as much
+ * as possible via compile time or command-line options.  Since I only have
+ * a few drives, you generally need to send me patches...
+ *
+ * ----------------------------------
+ * TO DO LIST:
+ * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
+ *   boot
+ *
+ * ----------------------------------
+ * 1.00  Oct 31, 1994 -- Initial version.
+ * 1.01  Nov  2, 1994 -- Fixed problem with starting request in
+ *                       cdrom_check_status.
+ * 1.03  Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
+ * (from mlord)       -- minor changes to cdrom_setup()
+ *                    -- renamed ide_dev_s to ide_drive_t, enable irq on command
+ * 2.00  Nov 27, 1994 -- Generalize packet command interface;
+ *                       add audio ioctls.
+ * 2.01  Dec  3, 1994 -- Rework packet command interface to handle devices
+ *                       which send an interrupt when ready for a command.
+ * 2.02  Dec 11, 1994 -- Cache the TOC in the driver.
+ *                       Don't use SCMD_PLAYAUDIO_TI; it's not included
+ *                       in the current version of ATAPI.
+ *                       Try to use LBA instead of track or MSF addressing
+ *                       when possible.
+ *                       Don't wait for READY_STAT.
+ * 2.03  Jan 10, 1995 -- Rewrite block read routines to handle block sizes
+ *                       other than 2k and to move multiple sectors in a
+ *                       single transaction.
+ * 2.04  Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
+ *                       Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
+ *                       help in figuring this out.  Ditto for Acer and
+ *                       Aztech drives, which seem to have the same problem.
+ * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
+ * 2.05  Jun  8, 1995 -- Don't attempt to retry after an illegal request
+ *                        or data protect error.
+ *                       Use HWIF and DEV_HWIF macros as in ide.c.
+ *                       Always try to do a request_sense after
+ *                        a failed command.
+ *                       Include an option to give textual descriptions
+ *                        of ATAPI errors.
+ *                       Fix a bug in handling the sector cache which
+ *                        showed up if the drive returned data in 512 byte
+ *                        blocks (like Pioneer drives).  Thanks to
+ *                        Richard Hirst <srh@gpt.co.uk> for diagnosing this.
+ *                       Properly supply the page number field in the
+ *                        MODE_SELECT command.
+ *                       PLAYAUDIO12 is broken on the Aztech; work around it.
+ * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
+ *                       (my apologies to Scott, but now ide-cd.c is independent)
+ * 3.00  Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
+ *                       Implement CDROMREADAUDIO ioctl (UNTESTED).
+ *                       Use input_ide_data() and output_ide_data().
+ *                       Add door locking.
+ *                       Fix usage count leak in cdrom_open, which happened
+ *                        when a read-write mount was attempted.
+ *                       Try to load the disk on open.
+ *                       Implement CDROMEJECT_SW ioctl (off by default).
+ *                       Read total cdrom capacity during open.
+ *                       Rearrange logic in cdrom_decode_status.  Issue
+ *                        request sense commands for failed packet commands
+ *                        from here instead of from cdrom_queue_packet_command.
+ *                        Fix a race condition in retrieving error information.
+ *                       Suppress printing normal unit attention errors and
+ *                        some drive not ready errors.
+ *                       Implement CDROMVOLREAD ioctl.
+ *                       Implement CDROMREADMODE1/2 ioctls.
+ *                       Fix race condition in setting up interrupt handlers
+ *                        when the `serialize' option is used.
+ * 3.01  Sep  2, 1995 -- Fix ordering of reenabling interrupts in
+ *                        cdrom_queue_request.
+ *                       Another try at using ide_[input,output]_data.
+ * 3.02  Sep 16, 1995 -- Stick total disk capacity in partition table as well.
+ *                       Make VERBOSE_IDE_CD_ERRORS dump failed command again.
+ *                       Dump out more information for ILLEGAL REQUEST errs.
+ *                       Fix handling of errors occurring before the
+ *                        packet command is transferred.
+ *                       Fix transfers with odd bytelengths.
+ * 3.03  Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
+ *                       `DCI-2S10' drives are broken too.
+ * 3.04  Nov 20, 1995 -- So are Vertos drives.
+ * 3.05  Dec  1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
+ * 3.06  Dec 16, 1995 -- Add support needed for partitions.
+ *                       More workarounds for Vertos bugs (based on patches
+ *                        from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
+ *                       Try to eliminate byteorder assumptions.
+ *                       Use atapi_cdrom_subchnl struct definition.
+ *                       Add STANDARD_ATAPI compilation option.
+ * 3.07  Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
+ *                        Vertos 300.
+ *                       Add NO_DOOR_LOCKING configuration option.
+ *                       Handle drive_cmd requests w/NULL args (for hdparm -t).
+ *                       Work around sporadic Sony55e audio play problem.
+ * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
+ *                        problem with "hde=cdrom" with no drive present.  -ml
+ * 3.08  Mar  6, 1996 -- More Vertos workarounds.
+ * 3.09  Apr  5, 1996 -- Add CDROMCLOSETRAY ioctl.
+ *                       Switch to using MSF addressing for audio commands.
+ *                       Reformat to match kernel tabbing style.
+ *                       Add CDROM_GET_UPC ioctl.
+ * 3.10  Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
+ * 3.11  Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
+ *                       to remove redundant verify_area calls.
+ * 3.12  May  7, 1996 -- Rudimentary changer support.  Based on patches
+ *                        from Gerhard Zuber <zuber@berlin.snafu.de>.
+ *                       Let open succeed even if there's no loaded disc.
+ * 3.13  May 19, 1996 -- Fixes for changer code.
+ * 3.14  May 29, 1996 -- Add work-around for Vertos 600.
+ *                        (From Hennus Bergman <hennus@sky.ow.nl>.)
+ * 3.15  July 2, 1996 -- Added support for Sanyo 3 CD changers
+ *                        from Ben Galliart <bgallia@luc.edu> with 
+ *                        special help from Jeff Lightfoot 
+ *                        <jeffml@pobox.com>
+ * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
+ * 3.16  Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
+ * 3.17  Sep 17, 1996 -- Tweak audio reads for some drives.
+ *                       Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
+ * 3.18  Oct 31, 1996 -- Added module and DMA support.
+ *                       
+ *                       
+ * 4.00  Nov 5, 1996   -- New ide-cd maintainer,
+ *                                 Erik B. Andersen <andersee@debian.org>
+ *                     -- Newer Creative drives don't always set the error
+ *                          register correctly.  Make sure we see media changes
+ *                          regardless.
+ *                     -- Integrate with generic cdrom driver.
+ *                     -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
+ *                          a patch from Ciro Cattuto <>.
+ *                     -- Call set_device_ro.
+ *                     -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
+ *                          ioctls, based on patch by Erik Andersen
+ *                     -- Add some probes of drive capability during setup.
+ *
+ * 4.01  Nov 11, 1996  -- Split into ide-cd.c and ide-cd.h
+ *                     -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE 
+ *                          ioctls in favor of a generalized approach 
+ *                          using the generic cdrom driver.
+ *                     -- Fully integrated with the 2.1.X kernel.
+ *                     -- Other stuff that I forgot (lots of changes)
+ *
+ * 4.02  Dec 01, 1996  -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
+ *                          to fix the drive door locking problems.
+ *
+ * 4.03  Dec 04, 1996  -- Added DSC overlap support.
+ * 4.04  Dec 29, 1996  -- Added CDROMREADRAW ioclt based on patch 
+ *                          by Ales Makarov (xmakarov@sun.felk.cvut.cz)
+ *
+ * 4.05  Nov 20, 1997  -- Modified to print more drive info on init
+ *                        Minor other changes
+ *                        Fix errors on CDROMSTOP (If you have a "Dolphin",
+ *                          you must define IHAVEADOLPHIN)
+ *                        Added identifier so new Sanyo CD-changer works
+ *                        Better detection if door locking isn't supported
+ *
+ * 4.06  Dec 17, 1997  -- fixed endless "tray open" messages  -ml
+ * 4.07  Dec 17, 1997  -- fallback to set pc->stat on "tray open"
+ * 4.08  Dec 18, 1997  -- spew less noise when tray is empty
+ *                     -- fix speed display for ACER 24X, 18X
+ * 4.09  Jan 04, 1998  -- fix handling of the last block so we return
+ *                         an end of file instead of an I/O error (Gadi)
+ * 4.10  Jan 24, 1998  -- fixed a bug so now changers can change to a new
+ *                         slot when there is no disc in the current slot.
+ *                     -- Fixed a memory leak where info->changer_info was
+ *                         malloc'ed but never free'd when closing the device.
+ *                     -- Cleaned up the global namespace a bit by making more
+ *                         functions static that should already have been.
+ * 4.11  Mar 12, 1998  -- Added support for the CDROM_SELECT_SPEED ioctl
+ *                         based on a patch for 2.0.33 by Jelle Foks 
+ *                         <jelle@scintilla.utwente.nl>, a patch for 2.0.33
+ *                         by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
+ *                         version, and my own efforts.  -erik
+ *                     -- Fixed a stupid bug which egcs was kind enough to
+ *                         inform me of where "Illegal mode for this track"
+ *                         was never returned due to a comparison on data
+ *                         types of limited range.
+ * 4.12  Mar 29, 1998  -- Fixed bug in CDROM_SELECT_SPEED so write speed is 
+ *                         now set ionly for CD-R and CD-RW drives.  I had 
+ *                         removed this support because it produced errors.
+ *                         It produced errors _only_ for non-writers. duh.
+ * 4.13  May 05, 1998  -- Suppress useless "in progress of becoming ready"
+ *                         messages, since this is not an error.
+ *                     -- Change error messages to be const
+ *                     -- Remove a "\t" which looks ugly in the syslogs
+ * 4.14  July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
+ *                         since the .pdf version doesn't seem to work...
+ *                     -- Updated the TODO list to something more current.
+ *
+ * 4.15  Aug 25, 1998  -- Updated ide-cd.h to respect mechine endianess, 
+ *                         patch thanks to "Eddie C. Dost" <ecd@skynet.be>
+ *
+ * 4.50  Oct 19, 1998  -- New maintainers!
+ *                         Jens Axboe <axboe@image.dk>
+ *                         Chris Zwilling <chris@cloudnet.com>
+ *
+ * 4.51  Dec 23, 1998  -- Jens Axboe <axboe@image.dk>
+ *                      - ide_cdrom_reset enabled since the ide subsystem
+ *                         handles resets fine now. <axboe@image.dk>
+ *                      - Transfer size fix for Samsung CD-ROMs, thanks to
+ *                        "Ville Hallik" <ville.hallik@mail.ee>.
+ *                      - other minor stuff.
+ *
+ * 4.52  Jan 19, 1999  -- Jens Axboe <axboe@image.dk>
+ *                      - Detect DVD-ROM/RAM drives
+ *
+ * 4.53  Feb 22, 1999   - Include other model Samsung and one Goldstar
+ *                         drive in transfer size limit.
+ *                      - Fix the I/O error when doing eject without a medium
+ *                         loaded on some drives.
+ *                      - CDROMREADMODE2 is now implemented through
+ *                         CDROMREADRAW, since many drives don't support
+ *                         MODE2 (even though ATAPI 2.6 says they must).
+ *                      - Added ignore parameter to ide-cd (as a module), eg
+ *                             insmod ide-cd ignore='hda hdb'
+ *                         Useful when using ide-cd in conjunction with
+ *                         ide-scsi. TODO: non-modular way of doing the
+ *                         same.
+ *
+ * 4.54  Aug 5, 1999   - Support for MMC2 class commands through the generic
+ *                       packet interface to cdrom.c.
+ *                     - Unified audio ioctl support, most of it.
+ *                     - cleaned up various deprecated verify_area().
+ *                     - Added ide_cdrom_packet() as the interface for
+ *                       the Uniform generic_packet().
+ *                     - bunch of other stuff, will fill in logs later.
+ *                     - report 1 slot for non-changers, like the other
+ *                       cd-rom drivers. don't report select disc for
+ *                       non-changers as well.
+ *                     - mask out audio playing, if the device can't do it.
+ *
+ * 4.55  Sep 1, 1999   - Eliminated the rest of the audio ioctls, except
+ *                       for CDROMREADTOC[ENTRY|HEADER]. Some of the drivers
+ *                       use this independently of the actual audio handling.
+ *                       They will disappear later when I get the time to
+ *                       do it cleanly.
+ *                     - Minimize the TOC reading - only do it when we
+ *                       know a media change has occurred.
+ *                     - Moved all the CDROMREADx ioctls to the Uniform layer.
+ *                     - Heiko Eissfeldt <heiko@colossus.escape.de> supplied
+ *                       some fixes for CDI.
+ *                     - CD-ROM leaving door locked fix from Andries
+ *                       Brouwer <Andries.Brouwer@cwi.nl>
+ *                     - Erik Andersen <andersen@xmission.com> unified
+ *                       commands across the various drivers and how
+ *                       sense errors are handled.
+ *
+ * 4.56  Sep 12, 1999  - Removed changer support - it is now in the
+ *                       Uniform layer.
+ *                     - Added partition based multisession handling.
+ *                     - Mode sense and mode select moved to the
+ *                       Uniform layer.
+ *                     - Fixed a problem with WPI CDS-32X drive - it
+ *                       failed the capabilities 
+ *
+ * 4.57  Apr 7, 2000   - Fixed sense reporting.
+ *                     - Fixed possible oops in ide_cdrom_get_last_session()
+ *                     - Fix locking mania and make ide_cdrom_reset relock
+ *                     - Stop spewing errors to log when magicdev polls with
+ *                       TEST_UNIT_READY on some drives.
+ *                     - Various fixes from Tobias Ringstrom:
+ *                       tray if it was locked prior to the reset.
+ *                       - cdrom_read_capacity returns one frame too little.
+ *                       - Fix real capacity reporting.
+ *
+ * 4.58  May 1, 2000   - Clean up ACER50 stuff.
+ *                     - Fix small problem with ide_cdrom_capacity
+ *
+ * 4.59  Aug 11, 2000  - Fix changer problem in cdrom_read_toc, we weren't
+ *                       correctly sensing a disc change.
+ *                     - Rearranged some code
+ *                     - Use extended sense on drives that support it for
+ *                       correctly reporting tray status -- from
+ *                       Michael D Johnson <johnsom@orst.edu>
+ *
+ *************************************************************************/
+#define IDECD_VERSION "4.59"
+
+#include <xeno/config.h>
+#include <xeno/module.h>
+#include <xeno/types.h>
+#include <xeno/kernel.h>
+#include <xeno/delay.h>
+#include <xeno/timer.h>
+#include <xeno/slab.h>
+#include <xeno/interrupt.h>
+#include <xeno/errno.h>
+#include <xeno/cdrom.h>
+#include <xeno/ide.h>
+// #include <xeno/completion.h>
+
+#include <asm/irq.h>
+#include <asm/io.h>
+#include <asm/byteorder.h>
+#include <asm/uaccess.h>
+#include <asm/unaligned.h>
+
+#include "ide-cd.h"
+
+/****************************************************************************
+ * Generic packet command support and error handling routines.
+ */
+
+/* Mark that we've seen a media change, and invalidate our internal
+   buffers. */
+static void cdrom_saw_media_change (ide_drive_t *drive)
+{
+       struct cdrom_info *info = drive->driver_data;
+       
+       CDROM_STATE_FLAGS (drive)->media_changed = 1;
+       CDROM_STATE_FLAGS (drive)->toc_valid = 0;
+       info->nsectors_buffered = 0;
+}
+
+static int cdrom_log_sense(ide_drive_t *drive, struct packet_command *pc,
+                          struct request_sense *sense)
+{
+       int log = 0;
+
+       if (sense == NULL || pc == NULL || pc->quiet)
+               return 0;
+
+       switch (sense->sense_key) {
+               case NO_SENSE: case RECOVERED_ERROR:
+                       break;
+               case NOT_READY:
+                       /*
+                        * don't care about tray state messages for
+                        * e.g. capacity commands or in-progress or
+                        * becoming ready
+                        */
+                       if (sense->asc == 0x3a || sense->asc == 0x04)
+                               break;
+                       log = 1;
+                       break;
+               case UNIT_ATTENTION:
+                       /*
+                        * Make good and sure we've seen this potential media
+                        * change. Some drives (i.e. Creative) fail to present
+                        * the correct sense key in the error register.
+                        */
+                       cdrom_saw_media_change(drive);
+                       break;
+               default:
+                       log = 1;
+                       break;
+       }
+       return log;
+}
+
+static
+void cdrom_analyze_sense_data(ide_drive_t *drive,
+                             struct packet_command *failed_command,
+                             struct request_sense *sense)
+{
+
+       if (!cdrom_log_sense(drive, failed_command, sense))
+               return;
+
+       /*
+        * If a read toc is executed for a CD-R or CD-RW medium where
+        * the first toc has not been recorded yet, it will fail with
+        * 05/24/00 (which is a confusing error)
+        */
+       if (failed_command && failed_command->c[0] == GPCMD_READ_TOC_PMA_ATIP)
+               if (sense->sense_key == 0x05 && sense->asc == 0x24)
+                       return;
+
+#if VERBOSE_IDE_CD_ERRORS
+       {
+               int i;
+               const char *s;
+               char buf[80];
+
+               printk ("ATAPI device %s:\n", drive->name);
+               if (sense->error_code==0x70)
+                       printk("  Error: ");
+               else if (sense->error_code==0x71)
+                       printk("  Deferred Error: ");
+               else if (sense->error_code == 0x7f)
+                       printk("  Vendor-specific Error: ");
+               else
+                       printk("  Unknown Error Type: ");
+
+               if (sense->sense_key < ARY_LEN(sense_key_texts))
+                       s = sense_key_texts[sense->sense_key];
+               else
+                       s = "bad sense key!";
+
+               printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
+
+               if (sense->asc == 0x40) {
+                       sprintf(buf, "Diagnostic failure on component 0x%02x",
+                                sense->ascq);
+                       s = buf;
+               } else {
+                       int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
+                       unsigned long key = (sense->sense_key << 16);
+                       key |= (sense->asc << 8);
+                       if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
+                               key |= sense->ascq;
+                       s = NULL;
+
+                       while (hi > lo) {
+                               mid = (lo + hi) / 2;
+                               if (sense_data_texts[mid].asc_ascq == key ||
+                                   sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
+                                       s = sense_data_texts[mid].text;
+                                       break;
+                               }
+                               else if (sense_data_texts[mid].asc_ascq > key)
+                                       hi = mid;
+                               else
+                                       lo = mid+1;
+                       }
+               }
+
+               if (s == NULL) {
+                       if (sense->asc > 0x80)
+                               s = "(vendor-specific error)";
+                       else
+                               s = "(reserved error code)";
+               }
+
+               printk("  %s -- (asc=0x%02x, ascq=0x%02x)\n",
+                       s, sense->asc, sense->ascq);
+
+               if (failed_command != NULL) {
+
+                       int lo=0, mid, hi= ARY_LEN (packet_command_texts);
+                       s = NULL;
+
+                       while (hi > lo) {
+                               mid = (lo + hi) / 2;
+                               if (packet_command_texts[mid].packet_command ==
+                                   failed_command->c[0]) {
+                                       s = packet_command_texts[mid].text;
+                                       break;
+                               }
+                               if (packet_command_texts[mid].packet_command >
+                                   failed_command->c[0])
+                                       hi = mid;
+                               else
+                                       lo = mid+1;
+                       }
+
+                       printk ("  The failed \"%s\" packet command was: \n  \"", s);
+                       for (i=0; i<sizeof (failed_command->c); i++)
+                               printk ("%02x ", failed_command->c[i]);
+                       printk ("\"\n");
+               }
+
+               /* The SKSV bit specifies validity of the sense_key_specific
+                * in the next two commands. It is bit 7 of the first byte.
+                * In the case of NOT_READY, if SKSV is set the drive can
+                * give us nice ETA readings.
+                */
+               if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
+                       int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
+                       printk("  Command is %02d%% complete\n", progress / 0xffff);
+
+               }
+
+               if (sense->sense_key == ILLEGAL_REQUEST &&
+                   (sense->sks[0] & 0x80) != 0) {
+                       printk("  Error in %s byte %d",
+                               (sense->sks[0] & 0x40) != 0 ?
+                               "command packet" : "command data",
+                               (sense->sks[1] << 8) + sense->sks[2]);
+
+                       if ((sense->sks[0] & 0x40) != 0)
+                               printk (" bit %d", sense->sks[0] & 0x07);
+
+                       printk ("\n");
+               }
+       }
+
+#else /* not VERBOSE_IDE_CD_ERRORS */
+
+       /* Suppress printing unit attention and `in progress of becoming ready'
+          errors when we're not being verbose. */
+
+       if (sense->sense_key == UNIT_ATTENTION ||
+           (sense->sense_key == NOT_READY && (sense->asc == 4 ||
+                                               sense->asc == 0x3a)))
+               return;
+
+       printk("%s: error code: 0x%02x  sense_key: 0x%02x  asc: 0x%02x  ascq: 0x%02x\n",
+               drive->name,
+               sense->error_code, sense->sense_key,
+               sense->asc, sense->ascq);
+#endif /* not VERBOSE_IDE_CD_ERRORS */
+}
+
+static void cdrom_queue_request_sense(ide_drive_t *drive, 
+                                     struct completion *wait,
+                                     struct request_sense *sense,
+                                     struct packet_command *failed_command)
+{
+       struct cdrom_info *info         = drive->driver_data;
+       struct packet_command *pc       = &info->request_sense_pc;
+       struct request *rq;
+
+       if (sense == NULL)
+               sense = &info->sense_data;
+
+       memset(pc, 0, sizeof(struct packet_command));
+       pc->c[0] = GPCMD_REQUEST_SENSE;
+       pc->c[4] = pc->buflen = 18;
+       pc->buffer = (char *) sense;
+       pc->sense = (struct request_sense *) failed_command;
+
+       /* stuff the sense request in front of our current request */
+       rq = &info->request_sense_request;
+       ide_init_drive_cmd(rq);
+       rq->cmd = REQUEST_SENSE_COMMAND;
+       rq->buffer = (char *) pc;
+       rq->waiting = wait;
+       (void) ide_do_drive_cmd(drive, rq, ide_preempt);
+}
+
+
+static void cdrom_end_request (int uptodate, ide_drive_t *drive)
+{
+       struct request *rq = HWGROUP(drive)->rq;
+
+       if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
+               struct packet_command *pc = (struct packet_command *) rq->buffer;
+               cdrom_analyze_sense_data(drive,
+                       (struct packet_command *) pc->sense,
+                       (struct request_sense *) (pc->buffer - pc->c[4]));
+       }
+       if (rq->cmd == READ || rq->cmd == WRITE)
+               if (!rq->current_nr_sectors)
+                       uptodate = 1;
+
+       ide_end_request (uptodate, HWGROUP(drive));
+}
+
+
+/* Returns 0 if the request should be continued.
+   Returns 1 if the request was ended. */
+static int cdrom_decode_status (ide_startstop_t *startstop, ide_drive_t *drive,
+                               int good_stat, int *stat_ret)
+{
+       struct request *rq = HWGROUP(drive)->rq;
+       int stat, err, sense_key;
+       struct packet_command *pc;
+       
+       /* Check for errors. */
+       stat = GET_STAT();
+       *stat_ret = stat;
+
+       if (OK_STAT (stat, good_stat, BAD_R_STAT))
+               return 0;
+
+       /* Get the IDE error register. */
+       err = GET_ERR();
+       sense_key = err >> 4;
+
+       if (rq == NULL) {
+               printk("%s: missing rq in cdrom_decode_status\n", drive->name);
+               *startstop = ide_stopped;
+               return 1;
+       }
+
+       if (rq->cmd == REQUEST_SENSE_COMMAND) {
+               /* We got an error trying to get sense info
+                  from the drive (probably while trying
+                  to recover from a former error).  Just give up. */
+
+               pc = (struct packet_command *) rq->buffer;
+               pc->stat = 1;
+               cdrom_end_request (1, drive);
+               *startstop = ide_error (drive, "request sense failure", stat);
+               return 1;
+
+       } else if (rq->cmd == PACKET_COMMAND) {
+               /* All other functions, except for READ. */
+               struct completion *wait = NULL;
+               pc = (struct packet_command *) rq->buffer;
+
+               /* Check for tray open. */
+               if (sense_key == NOT_READY) {
+                       cdrom_saw_media_change (drive);
+               } else if (sense_key == UNIT_ATTENTION) {
+                       /* Check for media change. */
+                       cdrom_saw_media_change (drive);
+                       /*printk("%s: media changed\n",drive->name);*/
+                       return 0;
+               } else if (!pc->quiet) {
+                       /* Otherwise, print an error. */
+                       ide_dump_status(drive, "packet command error", stat);
+               }
+               
+               /* Set the error flag and complete the request.
+                  Then, if we have a CHECK CONDITION status,
+                  queue a request sense command.  We must be careful,
+                  though: we don't want the thread in
+                  cdrom_queue_packet_command to wake up until
+                  the request sense has completed.  We do this
+                  by transferring the semaphore from the packet
+                  command request to the request sense request. */
+
+               if ((stat & ERR_STAT) != 0) {
+                       wait = rq->waiting;
+                       rq->waiting = NULL;
+               }
+
+               pc->stat = 1;
+               cdrom_end_request (1, drive);
+
+               if ((stat & ERR_STAT) != 0)
+                       cdrom_queue_request_sense(drive, wait, pc->sense, pc);
+       } else {
+               /* Handle errors from READ and WRITE requests. */
+
+               if (sense_key == NOT_READY) {
+                       /* Tray open. */
+                       cdrom_saw_media_change (drive);
+
+                       /* Fail the request. */
+                       printk ("%s: tray open\n", drive->name);
+                       cdrom_end_request (0, drive);
+               } else if (sense_key == UNIT_ATTENTION) {
+                       /* Media change. */
+                       cdrom_saw_media_change (drive);
+
+                       /* Arrange to retry the request.
+                          But be sure to give up if we've retried
+                          too many times. */
+                       if (++rq->errors > ERROR_MAX)
+                               cdrom_end_request (0, drive);
+               } else if (sense_key == ILLEGAL_REQUEST ||
+                          sense_key == DATA_PROTECT) {
+                       /* No point in retrying after an illegal
+                          request or data protect error.*/
+                       ide_dump_status (drive, "command error", stat);
+                       cdrom_end_request (0, drive);
+               } else if ((err & ~ABRT_ERR) != 0) {
+                       /* Go to the default handler
+                          for other errors. */
+                       *startstop = ide_error (drive, "cdrom_decode_status", stat);
+                       return 1;
+               } else if ((++rq->errors > ERROR_MAX)) {
+                       /* We've racked up too many retries.  Abort. */
+                       cdrom_end_request (0, drive);
+               }
+
+               /* If we got a CHECK_CONDITION status,
+                  queue a request sense command. */
+               if ((stat & ERR_STAT) != 0)
+                       cdrom_queue_request_sense(drive, NULL, NULL, NULL);
+       }
+
+       /* Retry, or handle the next request. */
+       *startstop = ide_stopped;
+       return 1;
+}
+
+static int cdrom_timer_expiry(ide_drive_t *drive)
+{
+       struct request *rq = HWGROUP(drive)->rq;
+       struct packet_command *pc = (struct packet_command *) rq->buffer;
+       unsigned long wait = 0;
+
+       /*
+        * Some commands are *slow* and normally take a long time to
+        * complete. Usually we can use the ATAPI "disconnect" to bypass
+        * this, but not all commands/drives support that. Let
+        * ide_timer_expiry keep polling us for these.
+        */
+       switch (pc->c[0]) {
+               case GPCMD_BLANK:
+               case GPCMD_FORMAT_UNIT:
+               case GPCMD_RESERVE_RZONE_TRACK:
+                       wait = WAIT_CMD;
+                       break;
+               default:
+                       wait = 0;
+                       break;
+       }
+       return wait;
+}
+
+/* Set up the device registers for transferring a packet command on DEV,
+   expecting to later transfer XFERLEN bytes.  HANDLER is the routine
+   which actually transfers the command to the drive.  If this is a
+   drq_interrupt device, this routine will arrange for HANDLER to be
+   called when the interrupt from the drive arrives.  Otherwise, HANDLER
+   will be called immediately after the drive is prepared for the transfer. */
+
+static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
+                                                 int xferlen,
+                                                 ide_handler_t *handler)
+{
+       ide_startstop_t startstop;
+       struct cdrom_info *info = drive->driver_data;
+
+       /* Wait for the controller to be idle. */
+       if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
+               return startstop;
+
+       if (info->dma) {
+               if (info->cmd == READ) {
+                       info->dma = !HWIF(drive)->dmaproc(ide_dma_read, drive);
+               } else if (info->cmd == WRITE) {
+                       info->dma = !HWIF(drive)->dmaproc(ide_dma_write, drive);
+               } else {
+                       printk("ide-cd: DMA set, but not allowed\n");
+               }
+       }
+
+       /* Set up the controller registers. */
+       OUT_BYTE (info->dma, IDE_FEATURE_REG);
+       OUT_BYTE (0, IDE_NSECTOR_REG);
+       OUT_BYTE (0, IDE_SECTOR_REG);
+
+       OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
+       OUT_BYTE (xferlen >> 8  , IDE_HCYL_REG);
+       if (IDE_CONTROL_REG)
+               OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
+       if (info->dma)
+               (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
+
+       if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
+               ide_set_handler (drive, handler, WAIT_CMD, cdrom_timer_expiry);
+               OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
+               return ide_started;
+       } else {
+               OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
+               return (*handler) (drive);
+       }
+}
+
+/* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
+   The device registers must have already been prepared
+   by cdrom_start_packet_command.
+   HANDLER is the interrupt handler to call when the command completes
+   or there's data ready. */
+/*
+ * changed 5 parameters to 3 for dvd-ram
+ * struct packet_command *pc; now packet_command_t *pc;
+ */
+static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
+                                         struct packet_command *pc,
+                                         ide_handler_t *handler)
+{
+       unsigned char *cmd_buf  = pc->c;
+       int cmd_len             = sizeof(pc->c);
+       unsigned int timeout    = pc->timeout;
+       ide_startstop_t startstop;
+
+       if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
+               /* Here we should have been called after receiving an interrupt
+                  from the device.  DRQ should how be set. */
+               int stat_dum;
+
+               /* Check for errors. */
+               if (cdrom_decode_status (&startstop, drive, DRQ_STAT, &stat_dum))
+                       return startstop;
+       } else {
+               /* Otherwise, we must wait for DRQ to get set. */
+               if (ide_wait_stat (&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
+                       return startstop;
+       }
+
+       /* Arm the interrupt handler. */
+       ide_set_handler (drive, handler, timeout, cdrom_timer_expiry);
+
+       /* Send the command to the device. */
+       atapi_output_bytes (drive, cmd_buf, cmd_len);
+       return ide_started;
+}
+
+/****************************************************************************
+ * Block read functions.
+ */
+
+/*
+ * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
+ * buffer.  Once the first sector is added, any subsequent sectors are
+ * assumed to be continuous (until the buffer is cleared).  For the first
+ * sector added, SECTOR is its sector number.  (SECTOR is then ignored until
+ * the buffer is cleared.)
+ */
+static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
+                                  int sectors_to_transfer)
+{
+       struct cdrom_info *info = drive->driver_data;
+
+       /* Number of sectors to read into the buffer. */
+       int sectors_to_buffer = MIN (sectors_to_transfer,
+                                    (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
+                                      info->nsectors_buffered);
+
+       char *dest;
+
+       /* If we couldn't get a buffer, don't try to buffer anything... */
+       if (info->buffer == NULL)
+               sectors_to_buffer = 0;
+
+       /* If this is the first sector in the buffer, remember its number. */
+       if (info->nsectors_buffered == 0)
+               info->sector_buffered = sector;
+
+       /* Read the data into the buffer. */
+       dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
+       while (sectors_to_buffer > 0) {
+               atapi_input_bytes (drive, dest, SECTOR_SIZE);
+               --sectors_to_buffer;
+               --sectors_to_transfer;
+               ++info->nsectors_buffered;
+               dest += SECTOR_SIZE;
+       }
+
+       /* Throw away any remaining data. */
+       while (sectors_to_transfer > 0) {
+               char dum[SECTOR_SIZE];
+               atapi_input_bytes (drive, dum, sizeof (dum));
+               --sectors_to_transfer;
+       }
+}
+
+/*
+ * Check the contents of the interrupt reason register from the cdrom
+ * and attempt to recover if there are problems.  Returns  0 if everything's
+ * ok; nonzero if the request has been terminated.
+ */
+static inline
+int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
+{
+       ireason &= 3;
+       if (ireason == 2) return 0;
+
+       if (ireason == 0) {
+               /* Whoops... The drive is expecting to receive data from us! */
+               printk ("%s: cdrom_read_intr: "
+                       "Drive wants to transfer data the wrong way!\n",
+                       drive->name);
+
+               /* Throw some data at the drive so it doesn't hang
+                  and quit this request. */
+               while (len > 0) {
+                       int dum = 0;
+                       atapi_output_bytes (drive, &dum, sizeof (dum));
+                       len -= sizeof (dum);
+               }
+       } else  if (ireason == 1) {
+               /* Some drives (ASUS) seem to tell us that status
+                * info is available. just get it and ignore.
+                */
+               GET_STAT();
+               return 0;
+       } else {
+               /* Drive wants a command packet, or invalid ireason... */
+               printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
+                       drive->name, ireason);
+       }
+
+       cdrom_end_request (0, drive);
+       return -1;
+}
+
+/*
+ * Interrupt routine.  Called when a read request has completed.
+ */
+static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
+{
+       int stat;
+       int ireason, len, sectors_to_transfer, nskip;
+       struct cdrom_info *info = drive->driver_data;
+       int i, dma = info->dma, dma_error = 0;
+       ide_startstop_t startstop;
+
+       struct request *rq = HWGROUP(drive)->rq;
+
+       /* Check for errors. */
+       if (dma) {
+               info->dma = 0;
+               if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive)))
+                       HWIF(drive)->dmaproc(ide_dma_off, drive);
+       }
+
+       if (cdrom_decode_status (&startstop, drive, 0, &stat))
+               return startstop;
+       if (dma) {
+               if (!dma_error) {
+                       for (i = rq->nr_sectors; i > 0;) {
+                               i -= rq->current_nr_sectors;
+                               ide_end_request(1, HWGROUP(drive));
+                       }
+                       return ide_stopped;
+               } else
+                       return ide_error (drive, "dma error", stat);
+       }
+
+       /* Read the interrupt reason and the transfer length. */
+       ireason = IN_BYTE (IDE_NSECTOR_REG);
+       len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
+
+       /* If DRQ is clear, the command has completed. */
+       if ((stat & DRQ_STAT) == 0) {
+               /* If we're not done filling the current buffer, complain.
+                  Otherwise, complete the command normally. */
+               if (rq->current_nr_sectors > 0) {
+                       printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
+                               drive->name, rq->current_nr_sectors);
+                       cdrom_end_request (0, drive);
+               } else
+                       cdrom_end_request (1, drive);
+               return ide_stopped;
+       }
+
+       /* Check that the drive is expecting to do the same thing we are. */
+       if (cdrom_read_check_ireason (drive, len, ireason))
+               return ide_stopped;
+
+       /* Assume that the drive will always provide data in multiples
+          of at least SECTOR_SIZE, as it gets hairy to keep track
+          of the transfers otherwise. */
+       if ((len % SECTOR_SIZE) != 0) {
+               printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
+                       drive->name, len);
+               if (CDROM_CONFIG_FLAGS (drive)->limit_nframes)
+                       printk ("  This drive is not supported by this version of the driver\n");
+               else {
+                       printk ("  Trying to limit transfer sizes\n");
+                       CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
+               }
+               cdrom_end_request (0, drive);
+               return ide_stopped;
+       }
+
+       /* The number of sectors we need to read from the drive. */
+       sectors_to_transfer = len / SECTOR_SIZE;
+
+       /* First, figure out if we need to bit-bucket
+          any of the leading sectors. */
+       nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
+                    sectors_to_transfer);
+
+       while (nskip > 0) {
+               /* We need to throw away a sector. */
+               char dum[SECTOR_SIZE];
+               atapi_input_bytes (drive, dum, sizeof (dum));
+
+               --rq->current_nr_sectors;
+               --nskip;
+               --sectors_to_transfer;
+       }
+
+       /* Now loop while we still have data to read from the drive. */
+       while (sectors_to_transfer > 0) {
+               int this_transfer;
+
+               /* If we've filled the present buffer but there's another
+                  chained buffer after it, move on. */
+               if (rq->current_nr_sectors == 0 && rq->nr_sectors)
+                       cdrom_end_request (1, drive);
+
+               /* If the buffers are full, cache the rest of the data in our
+                  internal buffer. */
+               if (rq->current_nr_sectors == 0) {
+                       cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
+                       sectors_to_transfer = 0;
+               } else {
+                       /* Transfer data to the buffers.
+                          Figure out how many sectors we can transfer
+                          to the current buffer. */
+                       this_transfer = MIN (sectors_to_transfer,
+                                            rq->current_nr_sectors);
+
+                       /* Read this_transfer sectors
+                          into the current buffer. */
+                       while (this_transfer > 0) {
+                               atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
+                               rq->buffer += SECTOR_SIZE;
+                               --rq->nr_sectors;
+                               --rq->current_nr_sectors;
+                               ++rq->sector;
+                               --this_transfer;
+                               --sectors_to_transfer;
+                       }
+               }
+       }
+
+       /* Done moving data!
+          Wait for another interrupt. */
+       ide_set_handler(drive, &cdrom_read_intr, WAIT_CMD, NULL);
+       return ide_started;
+}
+
+/*
+ * Try to satisfy some of the current read request from our cached data.
+ * Returns nonzero if the request has been completed, zero otherwise.
+ */
+static int cdrom_read_from_buffer (ide_drive_t *drive)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct request *rq = HWGROUP(drive)->rq;
+
+       /* Can't do anything if there's no buffer. */
+       if (info->buffer == NULL) return 0;
+
+       /* Loop while this request needs data and the next block is present
+          in our cache. */
+       while (rq->nr_sectors > 0 &&
+              rq->sector >= info->sector_buffered &&
+              rq->sector < info->sector_buffered + info->nsectors_buffered) {
+               if (rq->current_nr_sectors == 0)
+                       cdrom_end_request (1, drive);
+
+               memcpy (rq->buffer,
+                       info->buffer +
+                       (rq->sector - info->sector_buffered) * SECTOR_SIZE,
+                       SECTOR_SIZE);
+               rq->buffer += SECTOR_SIZE;
+               --rq->current_nr_sectors;
+               --rq->nr_sectors;
+               ++rq->sector;
+       }
+
+       /* If we've satisfied the current request,
+          terminate it successfully. */
+       if (rq->nr_sectors == 0) {
+               cdrom_end_request (1, drive);
+               return -1;
+       }
+
+       /* Move on to the next buffer if needed. */
+       if (rq->current_nr_sectors == 0)
+               cdrom_end_request (1, drive);
+
+       /* If this condition does not hold, then the kluge i use to
+          represent the number of sectors to skip at the start of a transfer
+          will fail.  I think that this will never happen, but let's be
+          paranoid and check. */
+       if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
+           (rq->sector % SECTORS_PER_FRAME) != 0) {
+               printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
+                       drive->name, rq->sector);
+               cdrom_end_request (0, drive);
+               return -1;
+       }
+
+       return 0;
+}
+
+/*
+ * Routine to send a read packet command to the drive.
+ * This is usually called directly from cdrom_start_read.
+ * However, for drq_interrupt devices, it is called from an interrupt
+ * when the drive is ready to accept the command.
+ */
+static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
+{
+       struct packet_command pc;
+       struct request *rq = HWGROUP(drive)->rq;
+       int nsect, sector, nframes, frame, nskip;
+
+       /* Number of sectors to transfer. */
+       nsect = rq->nr_sectors;
+
+       /* Starting sector. */
+       sector = rq->sector;
+
+       /* If the requested sector doesn't start on a cdrom block boundary,
+          we must adjust the start of the transfer so that it does,
+          and remember to skip the first few sectors.
+          If the CURRENT_NR_SECTORS field is larger than the size
+          of the buffer, it will mean that we're to skip a number
+          of sectors equal to the amount by which CURRENT_NR_SECTORS
+          is larger than the buffer size. */
+       nskip = (sector % SECTORS_PER_FRAME);
+       if (nskip > 0) {
+               /* Sanity check... */
+               if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS) &&
+                       (rq->sector % CD_FRAMESIZE != 0)) {
+                       printk ("%s: cdrom_start_read_continuation: buffer botch (%lu)\n",
+                               drive->name, rq->current_nr_sectors);
+                       cdrom_end_request (0, drive);
+                       return ide_stopped;
+               }
+               sector -= nskip;
+               nsect += nskip;
+               rq->current_nr_sectors += nskip;
+       }
+
+       /* Convert from sectors to cdrom blocks, rounding up the transfer
+          length if needed. */
+       nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
+       frame = sector / SECTORS_PER_FRAME;
+
+       /* Largest number of frames was can transfer at once is 64k-1. For
+          some drives we need to limit this even more. */
+       nframes = MIN (nframes, (CDROM_CONFIG_FLAGS (drive)->limit_nframes) ?
+               (65534 / CD_FRAMESIZE) : 65535);
+
+       /* Set up the command */
+       memset (&pc.c, 0, sizeof (pc.c));
+       pc.c[0] = GPCMD_READ_10;
+       pc.c[7] = (nframes >> 8);
+       pc.c[8] = (nframes & 0xff);
+       put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
+       pc.timeout = WAIT_CMD;
+
+       /* Send the command to the drive and return. */
+       return cdrom_transfer_packet_command(drive, &pc, &cdrom_read_intr);
+}
+
+
+#define IDECD_SEEK_THRESHOLD   (1000)                  /* 1000 blocks */
+#define IDECD_SEEK_TIMER       (5 * WAIT_MIN_SLEEP)    /* 100 ms */
+#define IDECD_SEEK_TIMEOUT     WAIT_CMD                        /* 10 sec */
+
+static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
+{
+       struct cdrom_info *info = drive->driver_data;
+       int stat;
+       static int retry = 10;
+       ide_startstop_t startstop;
+
+       if (cdrom_decode_status (&startstop, drive, 0, &stat))
+               return startstop;
+       CDROM_CONFIG_FLAGS(drive)->seeking = 1;
+
+       if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) {
+               if (--retry == 0) {
+                       /*
+                        * this condition is far too common, to bother
+                        * users about it
+                        */
+#if 0
+                       printk("%s: disabled DSC seek overlap\n", drive->name);
+#endif
+                       drive->dsc_overlap = 0;
+               }
+       }
+       return ide_stopped;
+}
+
+static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
+{
+       struct packet_command pc;
+       struct request *rq = HWGROUP(drive)->rq;
+       int sector, frame, nskip;
+
+       sector = rq->sector;
+       nskip = (sector % SECTORS_PER_FRAME);
+       if (nskip > 0)
+               sector -= nskip;
+       frame = sector / SECTORS_PER_FRAME;
+
+       memset (&pc.c, 0, sizeof (pc.c));
+       pc.c[0] = GPCMD_SEEK;
+       put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
+
+       pc.timeout = WAIT_CMD;
+       return cdrom_transfer_packet_command(drive, &pc, &cdrom_seek_intr);
+}
+
+static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
+{
+       struct cdrom_info *info = drive->driver_data;
+
+       info->dma = 0;
+       info->cmd = 0;
+       info->start_seek = jiffies;
+       return cdrom_start_packet_command (drive, 0, cdrom_start_seek_continuation);
+}
+
+static inline int cdrom_merge_requests(struct request *rq, struct request *nxt)
+{
+       int ret = 1;
+
+       /*
+        * partitions not really working, but better check anyway...
+        */
+       if (rq->cmd == nxt->cmd && rq->rq_dev == nxt->rq_dev) {
+               rq->nr_sectors += nxt->nr_sectors;
+               rq->hard_nr_sectors += nxt->nr_sectors;
+               rq->bhtail->b_reqnext = nxt->bh;
+               rq->bhtail = nxt->bhtail;
+               list_del(&nxt->queue);
+               blkdev_release_request(nxt);
+               ret = 0;
+       }
+
+       return ret;
+}
+
+/*
+ * the current request will always be the first one on the list
+ */
+static void cdrom_attempt_remerge(ide_drive_t *drive, struct request *rq)
+{
+       struct list_head *entry;
+       struct request *nxt;
+       unsigned long flags;
+
+       spin_lock_irqsave(&io_request_lock, flags);
+
+       while (1) {
+               entry = rq->queue.next;
+               if (entry == &drive->queue.queue_head)
+                       break;
+
+               nxt = blkdev_entry_to_request(entry);
+               if (rq->sector + rq->nr_sectors != nxt->sector)
+                       break;
+               else if (rq->nr_sectors + nxt->nr_sectors > SECTORS_MAX)
+                       break;
+
+               if (cdrom_merge_requests(rq, nxt))
+                       break;
+       }
+
+       spin_unlock_irqrestore(&io_request_lock, flags);
+}
+
+/* Fix up a possibly partially-processed request so that we can
+   start it over entirely, or even put it back on the request queue. */
+static void restore_request (struct request *rq)
+{
+       if (rq->buffer != rq->bh->b_data) {
+               int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
+               rq->buffer = rq->bh->b_data;
+               rq->nr_sectors += n;
+               rq->sector -= n;
+       }
+       rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
+       rq->hard_nr_sectors = rq->nr_sectors;
+       rq->hard_sector = rq->sector;
+}
+
+/*
+ * Start a read request from the CD-ROM.
+ */
+static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct request *rq = HWGROUP(drive)->rq;
+       int minor = MINOR (rq->rq_dev);
+
+       /* If the request is relative to a partition, fix it up to refer to the
+          absolute address.  */
+       if (minor & PARTN_MASK) {
+               rq->sector = block;
+               minor &= ~PARTN_MASK;
+               rq->rq_dev = MKDEV(MAJOR(rq->rq_dev), minor);
+       }
+
+       /* We may be retrying this request after an error.  Fix up
+          any weirdness which might be present in the request packet. */
+       restore_request(rq);
+
+       /* Satisfy whatever we can of this request from our cached sector. */
+       if (cdrom_read_from_buffer(drive))
+               return ide_stopped;
+
+       cdrom_attempt_remerge(drive, rq);
+
+       /* Clear the local sector buffer. */
+       info->nsectors_buffered = 0;
+
+       /* use dma, if possible. */
+       if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) &&
+                               (rq->nr_sectors % SECTORS_PER_FRAME == 0))
+               info->dma = 1;
+       else
+               info->dma = 0;
+
+       info->cmd = READ;
+       /* Start sending the read request to the drive. */
+       return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
+}
+
+/****************************************************************************
+ * Execute all other packet commands.
+ */
+
+/* Forward declarations. */
+static int cdrom_lockdoor(ide_drive_t *drive, int lockflag,
+                         struct request_sense *sense);
+
+/* Interrupt routine for packet command completion. */
+static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
+{
+       int ireason, len, stat, thislen;
+       struct request *rq = HWGROUP(drive)->rq;
+       struct packet_command *pc = (struct packet_command *)rq->buffer;
+       ide_startstop_t startstop;
+
+       /* Check for errors. */
+       if (cdrom_decode_status (&startstop, drive, 0, &stat))
+               return startstop;
+
+       /* Read the interrupt reason and the transfer length. */
+       ireason = IN_BYTE (IDE_NSECTOR_REG);
+       len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
+
+       /* If DRQ is clear, the command has completed.
+          Complain if we still have data left to transfer. */
+       if ((stat & DRQ_STAT) == 0) {
+               /* Some of the trailing request sense fields are optional, and
+                  some drives don't send them.  Sigh. */
+               if (pc->c[0] == GPCMD_REQUEST_SENSE &&
+                   pc->buflen > 0 &&
+                   pc->buflen <= 5) {
+                       while (pc->buflen > 0) {
+                               *pc->buffer++ = 0;
+                               --pc->buflen;
+                       }
+               }
+
+               if (pc->buflen == 0)
+                       cdrom_end_request (1, drive);
+               else {
+                       /* Comment this out, because this always happens 
+                          right after a reset occurs, and it is annoying to 
+                          always print expected stuff.  */
+                       /*
+                       printk ("%s: cdrom_pc_intr: data underrun %d\n",
+                               drive->name, pc->buflen);
+                       */
+                       pc->stat = 1;
+                       cdrom_end_request (1, drive);
+               }
+               return ide_stopped;
+       }
+
+       /* Figure out how much data to transfer. */
+       thislen = pc->buflen;
+       if (thislen > len) thislen = len;
+
+       /* The drive wants to be written to. */
+       if ((ireason & 3) == 0) {
+               /* Transfer the data. */
+               atapi_output_bytes (drive, pc->buffer, thislen);
+
+               /* If we haven't moved enough data to satisfy the drive,
+                  add some padding. */
+               while (len > thislen) {
+                       int dum = 0;
+                       atapi_output_bytes (drive, &dum, sizeof (dum));
+                       len -= sizeof (dum);
+               }
+
+               /* Keep count of how much data we've moved. */
+               pc->buffer += thislen;
+               pc->buflen -= thislen;
+       }
+
+       /* Same drill for reading. */
+       else if ((ireason & 3) == 2) {
+
+               /* Transfer the data. */
+               atapi_input_bytes (drive, pc->buffer, thislen);
+
+               /* If we haven't moved enough data to satisfy the drive,
+                  add some padding. */
+               while (len > thislen) {
+                       int dum = 0;
+                       atapi_input_bytes (drive, &dum, sizeof (dum));
+                       len -= sizeof (dum);
+               }
+
+               /* Keep count of how much data we've moved. */
+               pc->buffer += thislen;
+               pc->buflen -= thislen;
+       } else {
+               printk ("%s: cdrom_pc_intr: The drive "
+                       "appears confused (ireason = 0x%2x)\n",
+                       drive->name, ireason);
+               pc->stat = 1;
+       }
+
+       /* Now we wait for another interrupt. */
+       ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD, cdrom_timer_expiry);
+       return ide_started;
+}
+
+
+static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
+{
+       struct request *rq = HWGROUP(drive)->rq;
+       struct packet_command *pc = (struct packet_command *)rq->buffer;
+
+       if (!pc->timeout)
+               pc->timeout = WAIT_CMD;
+
+       /* Send the command to the drive and return. */
+       return cdrom_transfer_packet_command(drive, pc, &cdrom_pc_intr);
+}
+
+
+static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
+{
+       int len;
+       struct request *rq = HWGROUP(drive)->rq;
+       struct packet_command *pc = (struct packet_command *)rq->buffer;
+       struct cdrom_info *info = drive->driver_data;
+
+       info->dma = 0;
+       info->cmd = 0;
+       pc->stat = 0;
+       len = pc->buflen;
+
+       /* Start sending the command to the drive. */
+       return cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
+}
+
+
+/* Sleep for TIME jiffies.
+   Not to be called from an interrupt handler. */
+static
+void cdrom_sleep (int time)
+{
+       int sleep = time;
+
+       do {
+               set_current_state(TASK_INTERRUPTIBLE);
+               sleep = schedule_timeout(sleep);
+       } while (sleep);
+}
+
+static
+int cdrom_queue_packet_command(ide_drive_t *drive, struct packet_command *pc)
+{
+       struct request_sense sense;
+       struct request req;
+       int retries = 10;
+
+       if (pc->sense == NULL)
+               pc->sense = &sense;
+
+       /* Start of retry loop. */
+       do {
+               ide_init_drive_cmd (&req);
+               req.cmd = PACKET_COMMAND;
+               req.buffer = (char *)pc;
+               if (ide_do_drive_cmd (drive, &req, ide_wait)) {
+                       printk("%s: do_drive_cmd returned stat=%02x,err=%02x\n",
+                               drive->name, req.buffer[0], req.buffer[1]);
+                       /* FIXME: we should probably abort/retry or something */
+               }
+               if (pc->stat != 0) {
+                       /* The request failed.  Retry if it was due to a unit
+                          attention status
+                          (usually means media was changed). */
+                       struct request_sense *reqbuf = pc->sense;
+
+                       if (reqbuf->sense_key == UNIT_ATTENTION)
+                               cdrom_saw_media_change (drive);
+                       else if (reqbuf->sense_key == NOT_READY &&
+                                reqbuf->asc == 4 && reqbuf->ascq != 4) {
+                               /* The drive is in the process of loading
+                                  a disk.  Retry, but wait a little to give
+                                  the drive time to complete the load. */
+                               cdrom_sleep(2 * HZ);
+                       } else {
+                               /* Otherwise, don't retry. */
+                               retries = 0;
+                       }
+                       --retries;
+               }
+
+               /* End of retry loop. */
+       } while (pc->stat != 0 && retries >= 0);
+
+       /* Return an error if the command failed. */
+       return pc->stat ? -EIO : 0;
+}
+
+/*
+ * Write handling
+ */
+static inline int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
+{
+       /* Two notes about IDE interrupt reason here - 0 means that
+        * the drive wants to receive data from us, 2 means that
+        * the drive is expecting data from us.
+        */
+       ireason &= 3;
+
+       if (ireason == 2) {
+               /* Whoops... The drive wants to send data. */
+               printk("%s: cdrom_write_intr: wrong transfer direction!\n",
+                       drive->name);
+
+               /* Throw some data at the drive so it doesn't hang
+                  and quit this request. */
+               while (len > 0) {
+                       int dum = 0;
+                       atapi_output_bytes(drive, &dum, sizeof(dum));
+                       len -= sizeof(dum);
+               }
+       } else {
+               /* Drive wants a command packet, or invalid ireason... */
+               printk("%s: cdrom_write_intr: bad interrupt reason %d\n",
+                       drive->name, ireason);
+       }
+
+       cdrom_end_request(0, drive);
+       return 1;
+}
+
+static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
+{
+       int stat, ireason, len, sectors_to_transfer, uptodate;
+       struct cdrom_info *info = drive->driver_data;
+       int i, dma_error = 0, dma = info->dma;
+       ide_startstop_t startstop;
+
+       struct request *rq = HWGROUP(drive)->rq;
+
+       /* Check for errors. */
+       if (dma) {
+               info->dma = 0;
+               if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive))) {
+                       printk("ide-cd: write dma error\n");
+                       HWIF(drive)->dmaproc(ide_dma_off, drive);
+               }
+       }
+
+       if (cdrom_decode_status(&startstop, drive, 0, &stat)) {
+               printk("ide-cd: write_intr decode_status bad\n");
+               return startstop;
+       }
+       /*
+        * using dma, transfer is complete now
+        */
+       if (dma) {
+               if (dma_error)
+                       return ide_error(drive, "dma error", stat);
+
+               rq = HWGROUP(drive)->rq;
+               for (i = rq->nr_sectors; i > 0;) {
+                       i -= rq->current_nr_sectors;
+                       ide_end_request(1, HWGROUP(drive));
+               }
+               return ide_stopped;
+       }
+
+       /* Read the interrupt reason and the transfer length. */
+       ireason = IN_BYTE(IDE_NSECTOR_REG);
+       len = IN_BYTE(IDE_LCYL_REG) + 256 * IN_BYTE(IDE_HCYL_REG);
+
+       /* If DRQ is clear, the command has completed. */
+       if ((stat & DRQ_STAT) == 0) {
+               /* If we're not done writing, complain.
+                * Otherwise, complete the command normally.
+                */
+               uptodate = 1;
+               if (rq->current_nr_sectors > 0) {
+                       printk("%s: write_intr: data underrun (%ld blocks)\n",
+                       drive->name, rq->current_nr_sectors);
+                       uptodate = 0;
+               }
+               cdrom_end_request(uptodate, drive);
+               return ide_stopped;
+       }
+
+       /* Check that the drive is expecting to do the same thing we are. */
+       if (ireason & 3)
+               if (cdrom_write_check_ireason(drive, len, ireason))
+                       return ide_stopped;
+
+       sectors_to_transfer = len / SECTOR_SIZE;
+
+       /*
+        * now loop and write out the data
+        */
+       while (sectors_to_transfer > 0) {
+               int this_transfer;
+
+               if (!rq->current_nr_sectors) {
+                       printk("ide-cd: write_intr: oops\n");
+                       break;
+               }
+
+               /*
+                * Figure out how many sectors we can transfer
+                */
+               this_transfer = MIN(sectors_to_transfer,rq->current_nr_sectors);
+
+               while (this_transfer > 0) {
+                       atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
+                       rq->buffer += SECTOR_SIZE;
+                       --rq->nr_sectors;
+                       --rq->current_nr_sectors;
+                       ++rq->sector;
+                       --this_transfer;
+                       --sectors_to_transfer;
+               }
+
+               /*
+                * current buffer complete, move on
+                */
+               if (rq->current_nr_sectors == 0 && rq->nr_sectors)
+                       cdrom_end_request (1, drive);
+       }
+
+       /* re-arm handler */
+       ide_set_handler(drive, &cdrom_write_intr, 5 * WAIT_CMD, NULL);
+       return ide_started;
+}
+
+static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
+{
+       struct packet_command pc;       /* packet_command_t pc; */
+       struct request *rq = HWGROUP(drive)->rq;
+       unsigned nframes, frame;
+
+       nframes = rq->nr_sectors >> 2;
+       frame = rq->sector >> 2;
+
+       memset(&pc.c, 0, sizeof(pc.c));
+       /*
+        * we might as well use WRITE_12, but none of the device I have
+        * support the streaming feature anyway, so who cares.
+        */
+       pc.c[0] = GPCMD_WRITE_10;
+#if 0  /* the immediate bit */
+       pc.c[1] = 1 << 3;
+#endif
+       pc.c[7] = (nframes >> 8) & 0xff;
+       pc.c[8] = nframes & 0xff;
+       put_unaligned(cpu_to_be32(frame), (unsigned int *)&pc.c[2]);
+       pc.timeout = 2 * WAIT_CMD;
+
+       return cdrom_transfer_packet_command(drive, &pc, cdrom_write_intr);
+}
+
+static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
+{
+       struct cdrom_info *info = drive->driver_data;
+
+       /*
+        * writes *must* be 2kB frame aligned
+        */
+       if ((rq->nr_sectors & 3) || (rq->sector & 3)) {
+               cdrom_end_request(0, drive);
+               return ide_stopped;
+       }
+
+       /*
+        * for dvd-ram and such media, it's a really big deal to get
+        * big writes all the time. so scour the queue and attempt to
+        * remerge requests, often the plugging will not have had time
+        * to do this properly
+        */
+       cdrom_attempt_remerge(drive, rq);
+
+       info->nsectors_buffered = 0;
+
+        /* use dma, if possible. we don't need to check more, since we
+        * know that the transfer is always (at least!) 2KB aligned */
+       info->dma = drive->using_dma ? 1 : 0;
+       info->cmd = WRITE;
+
+       /* Start sending the read request to the drive. */
+       return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
+}
+
+/****************************************************************************
+ * cdrom driver request routine.
+ */
+static ide_startstop_t
+ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, unsigned long block)
+{
+       ide_startstop_t action;
+       struct cdrom_info *info = drive->driver_data;
+
+       switch (rq->cmd) {
+               case WRITE:
+               case READ: {
+                       if (CDROM_CONFIG_FLAGS(drive)->seeking) {
+                               unsigned long elpased = jiffies - info->start_seek;
+                               int stat = GET_STAT();
+
+                               if ((stat & SEEK_STAT) != SEEK_STAT) {
+                                       if (elpased < IDECD_SEEK_TIMEOUT) {
+                                               ide_stall_queue(drive, IDECD_SEEK_TIMER);
+                                               return ide_stopped;
+                                       }
+                                       printk ("%s: DSC timeout\n", drive->name);
+                               }
+                               CDROM_CONFIG_FLAGS(drive)->seeking = 0;
+                       }
+                       if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap)
+                               action = cdrom_start_seek (drive, block);
+                       else {
+                               if (rq->cmd == READ)
+                                       action = cdrom_start_read(drive, block);
+                               else
+                                       action = cdrom_start_write(drive, rq);
+                       }
+                       info->last_block = block;
+                       return action;
+               }
+
+               case PACKET_COMMAND:
+               case REQUEST_SENSE_COMMAND: {
+                       return cdrom_do_packet_command(drive);
+               }
+
+               case RESET_DRIVE_COMMAND: {
+                       cdrom_end_request(1, drive);
+                       return ide_do_reset(drive);
+               }
+
+               default: {
+                       printk("ide-cd: bad cmd %d\n", rq->cmd);
+                       cdrom_end_request(0, drive);
+                       return ide_stopped;
+               }
+       }
+}
+
+
+
+/****************************************************************************
+ * Ioctl handling.
+ *
+ * Routines which queue packet commands take as a final argument a pointer
+ * to a request_sense struct.  If execution of the command results
+ * in an error with a CHECK CONDITION status, this structure will be filled
+ * with the results of the subsequent request sense command.  The pointer
+ * can also be NULL, in which case no sense information is returned.
+ */
+
+#if ! STANDARD_ATAPI
+static inline
+int bin2bcd (int x)
+{
+       return (x%10) | ((x/10) << 4);
+}
+
+
+static inline
+int bcd2bin (int x)
+{
+       return (x >> 4) * 10 + (x & 0x0f);
+}
+
+static
+void msf_from_bcd (struct atapi_msf *msf)
+{
+       msf->minute = bcd2bin (msf->minute);
+       msf->second = bcd2bin (msf->second);
+       msf->frame  = bcd2bin (msf->frame);
+}
+
+#endif /* not STANDARD_ATAPI */
+
+
+static inline
+void lba_to_msf (int lba, byte *m, byte *s, byte *f)
+{
+       lba += CD_MSF_OFFSET;
+       lba &= 0xffffff;  /* negative lbas use only 24 bits */
+       *m = lba / (CD_SECS * CD_FRAMES);
+       lba %= (CD_SECS * CD_FRAMES);
+       *s = lba / CD_FRAMES;
+       *f = lba % CD_FRAMES;
+}
+
+
+static inline
+int msf_to_lba (byte m, byte s, byte f)
+{
+       return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
+}
+
+static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
+{
+       struct packet_command pc;
+       struct cdrom_info *info = drive->driver_data;
+       struct cdrom_device_info *cdi = &info->devinfo;
+
+       memset(&pc, 0, sizeof(pc));
+       pc.sense = sense;
+
+       pc.c[0] = GPCMD_TEST_UNIT_READY;
+
+#if ! STANDARD_ATAPI
+        /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to 
+           switch CDs instead of supporting the LOAD_UNLOAD opcode   */
+
+        pc.c[7] = cdi->sanyo_slot % 3;
+#endif /* not STANDARD_ATAPI */
+
+       return cdrom_queue_packet_command(drive, &pc);
+}
+
+
+/* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
+static int
+cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
+{
+       struct request_sense my_sense;
+       struct packet_command pc;
+       int stat;
+
+       if (sense == NULL)
+               sense = &my_sense;
+
+       /* If the drive cannot lock the door, just pretend. */
+       if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
+               stat = 0;
+       } else {
+               memset(&pc, 0, sizeof(pc));
+               pc.sense = sense;
+               pc.c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
+               pc.c[4] = lockflag ? 1 : 0;
+               stat = cdrom_queue_packet_command (drive, &pc);
+       }
+
+       /* If we got an illegal field error, the drive
+          probably cannot lock the door. */
+       if (stat != 0 &&
+           sense->sense_key == ILLEGAL_REQUEST &&
+           (sense->asc == 0x24 || sense->asc == 0x20)) {
+               printk ("%s: door locking not supported\n",
+                       drive->name);
+               CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
+               stat = 0;
+       }
+       
+       /* no medium, that's alright. */
+       if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
+               stat = 0;
+
+       if (stat == 0)
+               CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
+
+       return stat;
+}
+
+
+/* Eject the disk if EJECTFLAG is 0.
+   If EJECTFLAG is 1, try to reload the disk. */
+static int cdrom_eject(ide_drive_t *drive, int ejectflag,
+                      struct request_sense *sense)
+{
+       struct packet_command pc;
+
+       if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
+               return -EDRIVE_CANT_DO_THIS;
+       
+       /* reload fails on some drives, if the tray is locked */
+       if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
+               return 0;
+
+       memset(&pc, 0, sizeof (pc));
+       pc.sense = sense;
+
+       pc.c[0] = GPCMD_START_STOP_UNIT;
+       pc.c[4] = 0x02 + (ejectflag != 0);
+       return cdrom_queue_packet_command (drive, &pc);
+}
+
+static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
+                              struct request_sense *sense)
+{
+       struct {
+               __u32 lba;
+               __u32 blocklen;
+       } capbuf;
+
+       int stat;
+       struct packet_command pc;
+
+       memset(&pc, 0, sizeof(pc));
+       pc.sense = sense;
+
+       pc.c[0] = GPCMD_READ_CDVD_CAPACITY;
+       pc.buffer = (char *)&capbuf;
+       pc.buflen = sizeof(capbuf);
+
+       stat = cdrom_queue_packet_command(drive, &pc);
+       if (stat == 0)
+               *capacity = 1 + be32_to_cpu(capbuf.lba);
+
+       return stat;
+}
+
+static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
+                               int format, char *buf, int buflen,
+                               struct request_sense *sense)
+{
+       struct packet_command pc;
+
+       memset(&pc, 0, sizeof(pc));
+       pc.sense = sense;
+
+       pc.buffer =  buf;
+       pc.buflen = buflen;
+       pc.quiet = 1;
+       pc.c[0] = GPCMD_READ_TOC_PMA_ATIP;
+       pc.c[6] = trackno;
+       pc.c[7] = (buflen >> 8);
+       pc.c[8] = (buflen & 0xff);
+       pc.c[9] = (format << 6);
+
+       if (msf_flag)
+               pc.c[1] = 2;
+
+       return cdrom_queue_packet_command (drive, &pc);
+}
+
+
+/* Try to read the entire TOC for the disk into our internal buffer. */
+static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
+{
+       int minor, stat, ntracks, i;
+       kdev_t dev;
+       struct cdrom_info *info = drive->driver_data;
+       struct atapi_toc *toc = info->toc;
+       struct {
+               struct atapi_toc_header hdr;
+               struct atapi_toc_entry  ent;
+       } ms_tmp;
+
+       if (toc == NULL) {
+               /* Try to allocate space. */
+               toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
+                                                   GFP_KERNEL);
+               info->toc = toc;
+               if (toc == NULL) {
+                       printk ("%s: No cdrom TOC buffer!\n", drive->name);
+                       return -ENOMEM;
+               }
+       }
+
+       /* Check to see if the existing data is still valid.
+          If it is, just return. */
+       (void) cdrom_check_status(drive, sense);
+
+       if (CDROM_STATE_FLAGS(drive)->toc_valid)
+               return 0;
+
+       /* First read just the header, so we know how long the TOC is. */
+       stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
+                                   sizeof(struct atapi_toc_header), sense);
+       if (stat) return stat;
+
+#if ! STANDARD_ATAPI
+       if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
+               toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
+               toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
+       }
+#endif  /* not STANDARD_ATAPI */
+
+       ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
+       if (ntracks <= 0)
+               return -EIO;
+       if (ntracks > MAX_TRACKS)
+               ntracks = MAX_TRACKS;
+
+       /* Now read the whole schmeer. */
+       stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
+                                 (char *)&toc->hdr,
+                                  sizeof(struct atapi_toc_header) +
+                                  (ntracks + 1) *
+                                  sizeof(struct atapi_toc_entry), sense);
+
+       if (stat && toc->hdr.first_track > 1) {
+               /* Cds with CDI tracks only don't have any TOC entries,
+                  despite of this the returned values are
+                  first_track == last_track = number of CDI tracks + 1,
+                  so that this case is indistinguishable from the same
+                  layout plus an additional audio track.
+                  If we get an error for the regular case, we assume
+                  a CDI without additional audio tracks. In this case
+                  the readable TOC is empty (CDI tracks are not included)
+                  and only holds the Leadout entry. Heiko Eißfeldt */
+               ntracks = 0;
+               stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
+                                          (char *)&toc->hdr,
+                                          sizeof(struct atapi_toc_header) +
+                                          (ntracks + 1) *
+                                          sizeof(struct atapi_toc_entry),
+                                          sense);
+               if (stat) {
+                       return stat;
+               }
+#if ! STANDARD_ATAPI
+               if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
+                       toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
+                       toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
+               } else
+#endif  /* not STANDARD_ATAPI */
+               {
+                       toc->hdr.first_track = CDROM_LEADOUT;
+                       toc->hdr.last_track = CDROM_LEADOUT;
+               }
+       }
+
+       if (stat)
+               return stat;
+
+       toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
+
+#if ! STANDARD_ATAPI
+       if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
+               toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
+               toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
+       }
+#endif  /* not STANDARD_ATAPI */
+
+       for (i=0; i<=ntracks; i++) {
+#if ! STANDARD_ATAPI
+               if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
+                       if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
+                               toc->ent[i].track = bcd2bin (toc->ent[i].track);
+                       msf_from_bcd (&toc->ent[i].addr.msf);
+               }
+#endif  /* not STANDARD_ATAPI */
+               toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
+                                                  toc->ent[i].addr.msf.second,
+                                                  toc->ent[i].addr.msf.frame);
+       }
+
+       /* Read the multisession information. */
+       if (toc->hdr.first_track != CDROM_LEADOUT) {
+               /* Read the multisession information. */
+               stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
+                                          sizeof(ms_tmp), sense);
+               if (stat) return stat;
+       } else {
+               ms_tmp.ent.addr.msf.minute = 0;
+               ms_tmp.ent.addr.msf.second = 2;
+               ms_tmp.ent.addr.msf.frame  = 0;
+               ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
+       }
+
+#if ! STANDARD_ATAPI
+       if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
+               msf_from_bcd (&ms_tmp.ent.addr.msf);
+#endif  /* not STANDARD_ATAPI */
+
+       toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
+                                           ms_tmp.ent.addr.msf.second,
+                                           ms_tmp.ent.addr.msf.frame);
+
+       toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
+
+       /* Now try to get the total cdrom capacity. */
+       minor = (drive->select.b.unit) << PARTN_BITS;
+       dev = MKDEV(HWIF(drive)->major, minor);
+       stat = cdrom_get_last_written(dev, &toc->capacity);
+       if (stat)
+               stat = cdrom_read_capacity(drive, &toc->capacity, sense);
+       if (stat)
+               toc->capacity = 0x1fffff;
+
+       HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS] = (toc->capacity * SECTORS_PER_FRAME) >> (BLOCK_SIZE_BITS - 9);
+       drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
+
+       /* Remember that we've read this stuff. */
+       CDROM_STATE_FLAGS (drive)->toc_valid = 1;
+
+       return 0;
+}
+
+
+static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
+                                int buflen, struct request_sense *sense)
+{
+       struct packet_command pc;
+
+       memset(&pc, 0, sizeof(pc));
+       pc.sense = sense;
+
+       pc.buffer = buf;
+       pc.buflen = buflen;
+       pc.c[0] = GPCMD_READ_SUBCHANNEL;
+       pc.c[1] = 2;     /* MSF addressing */
+       pc.c[2] = 0x40;  /* request subQ data */
+       pc.c[3] = format;
+       pc.c[7] = (buflen >> 8);
+       pc.c[8] = (buflen & 0xff);
+       return cdrom_queue_packet_command(drive, &pc);
+}
+
+/* ATAPI cdrom drives are free to select the speed you request or any slower
+   rate :-( Requesting too fast a speed will _not_ produce an error. */
+static int cdrom_select_speed(ide_drive_t *drive, int speed,
+                             struct request_sense *sense)
+{
+       struct packet_command pc;
+       memset(&pc, 0, sizeof(pc));
+       pc.sense = sense;
+
+       if (speed == 0)
+               speed = 0xffff; /* set to max */
+       else
+               speed *= 177;   /* Nx to kbytes/s */
+
+       pc.c[0] = GPCMD_SET_SPEED;
+       /* Read Drive speed in kbytes/second MSB */
+       pc.c[2] = (speed >> 8) & 0xff;  
+       /* Read Drive speed in kbytes/second LSB */
+       pc.c[3] = speed & 0xff;
+       if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
+           CDROM_CONFIG_FLAGS(drive)->cd_rw ||
+           CDROM_CONFIG_FLAGS(drive)->dvd_r) {
+               /* Write Drive speed in kbytes/second MSB */
+               pc.c[4] = (speed >> 8) & 0xff;
+               /* Write Drive speed in kbytes/second LSB */
+               pc.c[5] = speed & 0xff;
+       }
+
+       return cdrom_queue_packet_command(drive, &pc);
+}
+
+static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
+{
+       struct request_sense sense;
+       struct packet_command pc;
+
+       memset(&pc, 0, sizeof (pc));
+       pc.sense = &sense;
+
+       pc.c[0] = GPCMD_PLAY_AUDIO_MSF;
+       lba_to_msf(lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
+       lba_to_msf(lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
+
+       return cdrom_queue_packet_command(drive, &pc);
+}
+
+static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
+                               struct atapi_toc_entry **ent)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct atapi_toc *toc = info->toc;
+       int ntracks;
+
+       /*
+        * don't serve cached data, if the toc isn't valid
+        */
+       if (!CDROM_STATE_FLAGS(drive)->toc_valid)
+               return -EINVAL;
+
+       /* Check validity of requested track number. */
+       ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
+       if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
+       if (track == CDROM_LEADOUT)
+               *ent = &toc->ent[ntracks];
+       else if (track < toc->hdr.first_track ||
+                track > toc->hdr.last_track)
+               return -EINVAL;
+       else
+               *ent = &toc->ent[track - toc->hdr.first_track];
+
+       return 0;
+}
+
+/* the generic packet interface to cdrom.c */
+static int ide_cdrom_packet(struct cdrom_device_info *cdi,
+                           struct cdrom_generic_command *cgc)
+{
+       struct packet_command pc;
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+
+       if (cgc->timeout <= 0)
+               cgc->timeout = WAIT_CMD;
+
+       /* here we queue the commands from the uniform CD-ROM
+          layer. the packet must be complete, as we do not
+          touch it at all. */
+       memset(&pc, 0, sizeof(pc));
+       memcpy(pc.c, cgc->cmd, CDROM_PACKET_SIZE);
+       pc.buffer = cgc->buffer;
+       pc.buflen = cgc->buflen;
+       pc.quiet = cgc->quiet;
+       pc.timeout = cgc->timeout;
+       pc.sense = cgc->sense;
+       return cgc->stat = cdrom_queue_packet_command(drive, &pc);
+}
+
+static
+int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
+                        unsigned int cmd, unsigned long arg)
+{
+       struct cdrom_generic_command cgc;
+       char buffer[16];
+       int stat;
+
+       init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
+
+       /* These will be moved into the Uniform layer shortly... */
+       switch (cmd) {
+       case CDROMSETSPINDOWN: {
+               char spindown;
+               if (copy_from_user(&spindown, (void *) arg, sizeof(char)))
+                       return -EFAULT;
+                if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
+                       return stat;
+
+               buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
+
+               return cdrom_mode_select(cdi, &cgc);
+       } 
+       case CDROMGETSPINDOWN: {
+               char spindown;
+                if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
+                       return stat;
+               spindown = buffer[11] & 0x0f;
+               if (copy_to_user((void *) arg, &spindown, sizeof (char)))
+                       return -EFAULT;
+               return 0;
+       }
+  
+       default:
+               return -EINVAL;
+       }
+
+}
+
+static
+int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
+                          unsigned int cmd, void *arg)
+                          
+{
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+       struct cdrom_info *info = drive->driver_data;
+       int stat;
+
+       switch (cmd) {
+       /*
+        * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
+        * atapi doesn't support it
+        */
+       case CDROMPLAYTRKIND: {
+               unsigned long lba_start, lba_end;
+               struct cdrom_ti *ti = (struct cdrom_ti *)arg;
+               struct atapi_toc_entry *first_toc, *last_toc;
+
+               stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
+               if (stat)
+                       return stat;
+
+               stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
+               if (stat)
+                       return stat;
+
+               if (ti->cdti_trk1 != CDROM_LEADOUT)
+                       ++last_toc;
+               lba_start = first_toc->addr.lba;
+               lba_end   = last_toc->addr.lba;
+
+               if (lba_end <= lba_start)
+                       return -EINVAL;
+
+               return cdrom_play_audio(drive, lba_start, lba_end);
+       }
+
+       case CDROMREADTOCHDR: {
+               struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
+               struct atapi_toc *toc;
+
+               /* Make sure our saved TOC is valid. */
+               stat = cdrom_read_toc(drive, NULL);
+               if (stat) return stat;
+
+               toc = info->toc;
+               tochdr->cdth_trk0 = toc->hdr.first_track;
+               tochdr->cdth_trk1 = toc->hdr.last_track;
+
+               return 0;
+       }
+
+       case CDROMREADTOCENTRY: {
+               struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
+               struct atapi_toc_entry *toce;
+
+               stat = cdrom_get_toc_entry (drive, tocentry->cdte_track, &toce);
+               if (stat) return stat;
+
+               tocentry->cdte_ctrl = toce->control;
+               tocentry->cdte_adr  = toce->adr;
+               if (tocentry->cdte_format == CDROM_MSF) {
+                       lba_to_msf (toce->addr.lba,
+                                  &tocentry->cdte_addr.msf.minute,
+                                  &tocentry->cdte_addr.msf.second,
+                                  &tocentry->cdte_addr.msf.frame);
+               } else
+                       tocentry->cdte_addr.lba = toce->addr.lba;
+
+               return 0;
+       }
+
+       default:
+               return -EINVAL;
+       }
+}
+
+static
+int ide_cdrom_reset (struct cdrom_device_info *cdi)
+{
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+       struct request_sense sense;
+       struct request req;
+       int ret;
+
+       ide_init_drive_cmd (&req);
+       req.cmd = RESET_DRIVE_COMMAND;
+       ret = ide_do_drive_cmd(drive, &req, ide_wait);
+
+       /*
+        * A reset will unlock the door. If it was previously locked,
+        * lock it again.
+        */
+       if (CDROM_STATE_FLAGS(drive)->door_locked)
+               (void) cdrom_lockdoor(drive, 1, &sense);
+
+       return ret;
+}
+
+
+static
+int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
+{
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+       struct request_sense sense;
+
+       if (position) {
+               int stat = cdrom_lockdoor(drive, 0, &sense);
+               if (stat) return stat;
+       }
+
+       return cdrom_eject(drive, !position, &sense);
+}
+
+static
+int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
+{
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+       return cdrom_lockdoor(drive, lock, NULL);
+}
+
+static
+int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
+{
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+       struct request_sense sense;
+       int stat;
+
+       if ((stat = cdrom_select_speed (drive, speed, &sense)) < 0)
+               return stat;
+
+        cdi->speed = CDROM_STATE_FLAGS (drive)->current_speed;
+        return 0;
+}
+
+static
+int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
+{
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+
+       if (slot_nr == CDSL_CURRENT) {
+               struct request_sense sense;
+               int stat = cdrom_check_status(drive, &sense);
+               if (stat == 0 || sense.sense_key == UNIT_ATTENTION)
+                       return CDS_DISC_OK;
+
+               if (sense.sense_key == NOT_READY && sense.asc == 0x04 &&
+                   sense.ascq == 0x04)
+                       return CDS_DISC_OK;
+
+
+               /*
+                * If not using Mt Fuji extended media tray reports,
+                * just return TRAY_OPEN since ATAPI doesn't provide
+                * any other way to detect this...
+                */
+               if (sense.sense_key == NOT_READY) {
+                       if (sense.asc == 0x3a && sense.ascq == 1)
+                               return CDS_NO_DISC;
+                       else
+                               return CDS_TRAY_OPEN;
+               }
+
+               return CDS_DRIVE_NOT_READY;
+       }
+       return -EINVAL;
+}
+
+static
+int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
+                               struct cdrom_multisession *ms_info)
+{
+       struct atapi_toc *toc;
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+       struct cdrom_info *info = drive->driver_data;
+       struct request_sense sense;
+       int ret;
+
+       if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
+               if ((ret = cdrom_read_toc(drive, &sense)))
+                       return ret;
+
+       toc = info->toc;
+       ms_info->addr.lba = toc->last_session_lba;
+       ms_info->xa_flag = toc->xa_flag;
+
+       return 0;
+}
+
+static
+int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
+                      struct cdrom_mcn *mcn_info)
+{
+       int stat;
+       char mcnbuf[24];
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+
+/* get MCN */
+       if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
+               return stat;
+
+       memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
+               sizeof (mcn_info->medium_catalog_number)-1);
+       mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
+               = '\0';
+
+       return 0;
+}
+
+
+
+/****************************************************************************
+ * Other driver requests (open, close, check media change).
+ */
+
+static
+int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
+                                      int slot_nr)
+{
+       ide_drive_t *drive = (ide_drive_t*) cdi->handle;
+       int retval;
+       
+       if (slot_nr == CDSL_CURRENT) {
+               (void) cdrom_check_status(drive, NULL);
+               retval = CDROM_STATE_FLAGS (drive)->media_changed;
+               CDROM_STATE_FLAGS (drive)->media_changed = 0;
+               return retval;
+       } else {
+               return -EINVAL;
+       }
+}
+
+
+static
+int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
+{
+       return 0;
+}
+
+
+/*
+ * Close down the device.  Invalidate all cached blocks.
+ */
+
+static
+void ide_cdrom_release_real (struct cdrom_device_info *cdi)
+{
+}
+
+
+
+/****************************************************************************
+ * Device initialization.
+ */
+static struct cdrom_device_ops ide_cdrom_dops = {
+       open:                   ide_cdrom_open_real,
+       release:                ide_cdrom_release_real,
+       drive_status:           ide_cdrom_drive_status,
+       media_changed:          ide_cdrom_check_media_change_real,
+       tray_move:              ide_cdrom_tray_move,
+       lock_door:              ide_cdrom_lock_door,
+       select_speed:           ide_cdrom_select_speed,
+       get_last_session:       ide_cdrom_get_last_session,
+       get_mcn:                ide_cdrom_get_mcn,
+       reset:                  ide_cdrom_reset,
+       audio_ioctl:            ide_cdrom_audio_ioctl,
+       dev_ioctl:              ide_cdrom_dev_ioctl,
+       capability:             CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
+                               CDC_SELECT_SPEED | CDC_SELECT_DISC |
+                               CDC_MULTI_SESSION | CDC_MCN |
+                               CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
+                               CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
+                               CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
+                               CDC_GENERIC_PACKET,
+       generic_packet:         ide_cdrom_packet,
+};
+
+static int ide_cdrom_register (ide_drive_t *drive, int nslots)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct cdrom_device_info *devinfo = &info->devinfo;
+       int minor = (drive->select.b.unit) << PARTN_BITS;
+
+       devinfo->dev = MKDEV (HWIF(drive)->major, minor);
+       devinfo->ops = &ide_cdrom_dops;
+       devinfo->mask = 0;
+       *(int *)&devinfo->speed = CDROM_STATE_FLAGS (drive)->current_speed;
+       *(int *)&devinfo->capacity = nslots;
+       devinfo->handle = (void *) drive;
+       strcpy(devinfo->name, drive->name);
+       
+       /* set capability mask to match the probe. */
+       if (!CDROM_CONFIG_FLAGS (drive)->cd_r)
+               devinfo->mask |= CDC_CD_R;
+       if (!CDROM_CONFIG_FLAGS (drive)->cd_rw)
+               devinfo->mask |= CDC_CD_RW;
+       if (!CDROM_CONFIG_FLAGS (drive)->dvd)
+               devinfo->mask |= CDC_DVD;
+       if (!CDROM_CONFIG_FLAGS (drive)->dvd_r)
+               devinfo->mask |= CDC_DVD_R;
+       if (!CDROM_CONFIG_FLAGS (drive)->dvd_ram)
+               devinfo->mask |= CDC_DVD_RAM;
+       if (!CDROM_CONFIG_FLAGS (drive)->is_changer)
+               devinfo->mask |= CDC_SELECT_DISC;
+       if (!CDROM_CONFIG_FLAGS (drive)->audio_play)
+               devinfo->mask |= CDC_PLAY_AUDIO;
+       if (!CDROM_CONFIG_FLAGS (drive)->close_tray)
+               devinfo->mask |= CDC_CLOSE_TRAY;
+
+#if 0
+       devinfo->de = devfs_register(drive->de, "cd", DEVFS_FL_DEFAULT,
+                                    HWIF(drive)->major, minor,
+                                    S_IFBLK | S_IRUGO | S_IWUGO,
+                                    ide_fops, NULL);
+#endif
+
+       return register_cdrom(devinfo);
+}
+
+static
+int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct cdrom_device_info *cdi = &info->devinfo;
+       struct cdrom_generic_command cgc;
+       int stat, attempts = 3, size = sizeof(*cap);
+
+       /*
+        * ACER50 (and others?) require the full spec length mode sense
+        * page capabilities size, but older drives break.
+        */
+       if (drive->id) {
+               if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
+                   !strcmp(drive->id->model, "WPI CDS-32X")))
+                       size -= sizeof(cap->pad);
+       }
+
+       /* we have to cheat a little here. the packet will eventually
+        * be queued with ide_cdrom_packet(), which extracts the
+        * drive from cdi->handle. Since this device hasn't been
+        * registered with the Uniform layer yet, it can't do this.
+        * Same goes for cdi->ops.
+        */
+       cdi->handle = (ide_drive_t *) drive;
+       cdi->ops = &ide_cdrom_dops;
+       init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
+       do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
+               stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
+               if (!stat)
+                       break;
+       } while (--attempts);
+       return stat;
+}
+
+static
+int ide_cdrom_probe_capabilities (ide_drive_t *drive)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct cdrom_device_info *cdi = &info->devinfo;
+       struct atapi_capabilities_page cap;
+       int nslots = 1;
+
+       if (CDROM_CONFIG_FLAGS (drive)->nec260) {
+               CDROM_CONFIG_FLAGS (drive)->no_eject = 0;                       
+               CDROM_CONFIG_FLAGS (drive)->audio_play = 1;       
+               return nslots;
+       }
+
+       if (ide_cdrom_get_capabilities(drive, &cap))
+               return 0;
+
+       if (cap.lock == 0)
+               CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
+       if (cap.eject)
+               CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
+       if (cap.cd_r_write)
+               CDROM_CONFIG_FLAGS (drive)->cd_r = 1;
+       if (cap.cd_rw_write)
+               CDROM_CONFIG_FLAGS (drive)->cd_rw = 1;
+       if (cap.test_write)
+               CDROM_CONFIG_FLAGS (drive)->test_write = 1;
+       if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
+               CDROM_CONFIG_FLAGS (drive)->dvd = 1;
+       if (cap.dvd_ram_write)
+               CDROM_CONFIG_FLAGS (drive)->dvd_ram = 1;
+       if (cap.dvd_r_write)
+               CDROM_CONFIG_FLAGS (drive)->dvd_r = 1;
+       if (cap.audio_play)
+               CDROM_CONFIG_FLAGS (drive)->audio_play = 1;
+       if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
+               CDROM_CONFIG_FLAGS (drive)->close_tray = 0;
+
+#if ! STANDARD_ATAPI
+       if (cdi->sanyo_slot > 0) {
+               CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
+               nslots = 3;
+       }
+
+       else
+#endif /* not STANDARD_ATAPI */
+       if (cap.mechtype == mechtype_individual_changer ||
+           cap.mechtype == mechtype_cartridge_changer) {
+               if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
+                       CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
+                       CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 1;
+               }
+       }
+
+       /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
+       if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
+               CDROM_STATE_FLAGS (drive)->current_speed  = 
+                       (((unsigned int)cap.curspeed) + (176/2)) / 176;
+               CDROM_CONFIG_FLAGS (drive)->max_speed = 
+                       (((unsigned int)cap.maxspeed) + (176/2)) / 176;
+       } else {
+               CDROM_STATE_FLAGS (drive)->current_speed  = 
+                       (ntohs(cap.curspeed) + (176/2)) / 176;
+               CDROM_CONFIG_FLAGS (drive)->max_speed = 
+                       (ntohs(cap.maxspeed) + (176/2)) / 176;
+       }
+
+       /* don't print speed if the drive reported 0.
+        */
+       printk("%s: ATAPI", drive->name);
+       if (CDROM_CONFIG_FLAGS(drive)->max_speed)
+               printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
+       printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
+
+       if (CDROM_CONFIG_FLAGS (drive)->dvd_r|CDROM_CONFIG_FLAGS (drive)->dvd_ram)
+               printk (" DVD%s%s", 
+               (CDROM_CONFIG_FLAGS (drive)->dvd_r)? "-R" : "", 
+               (CDROM_CONFIG_FLAGS (drive)->dvd_ram)? "-RAM" : "");
+
+        if (CDROM_CONFIG_FLAGS (drive)->cd_r|CDROM_CONFIG_FLAGS (drive)->cd_rw) 
+               printk (" CD%s%s", 
+               (CDROM_CONFIG_FLAGS (drive)->cd_r)? "-R" : "", 
+               (CDROM_CONFIG_FLAGS (drive)->cd_rw)? "/RW" : "");
+
+        if (CDROM_CONFIG_FLAGS (drive)->is_changer) 
+               printk (" changer w/%d slots", nslots);
+        else   
+               printk (" drive");
+
+       printk (", %dkB Cache", be16_to_cpu(cap.buffer_size));
+
+#ifdef CONFIG_BLK_DEV_IDEDMA
+       if (drive->using_dma)
+               (void) HWIF(drive)->dmaproc(ide_dma_verbose, drive);
+#endif /* CONFIG_BLK_DEV_IDEDMA */
+       printk("\n");
+
+       return nslots;
+}
+
+static void ide_cdrom_add_settings(ide_drive_t *drive)
+{
+#if 0 
+       int major = HWIF(drive)->major;
+       int minor = drive->select.b.unit << PARTN_BITS;
+
+       ide_add_setting(drive,  "breada_readahead",     SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
+       ide_add_setting(drive,  "file_readahead",       SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
+       ide_add_setting(drive,  "max_kb_per_request",   SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
+#endif
+       ide_add_setting(drive,  "dsc_overlap",          SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
+}
+
+static
+int ide_cdrom_setup (ide_drive_t *drive)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct cdrom_device_info *cdi = &info->devinfo;
+       int minor = drive->select.b.unit << PARTN_BITS;
+       int nslots;
+
+       /*
+        * default to read-only always and fix latter at the bottom
+        */
+       set_device_ro(MKDEV(HWIF(drive)->major, minor), 1);
+#if 0
+       set_blocksize(MKDEV(HWIF(drive)->major, minor), CD_FRAMESIZE);
+#endif
+
+       drive->special.all      = 0;
+       drive->ready_stat       = 0;
+
+       CDROM_STATE_FLAGS (drive)->media_changed = 1;
+       CDROM_STATE_FLAGS (drive)->toc_valid     = 0;
+       CDROM_STATE_FLAGS (drive)->door_locked   = 0;
+
+#if NO_DOOR_LOCKING
+       CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
+#else
+       CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
+#endif
+
+       if (drive->id != NULL)
+               CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
+                       ((drive->id->config & 0x0060) == 0x20);
+       else
+               CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
+
+       CDROM_CONFIG_FLAGS (drive)->is_changer = 0;
+       CDROM_CONFIG_FLAGS (drive)->cd_r = 0;
+       CDROM_CONFIG_FLAGS (drive)->cd_rw = 0;
+       CDROM_CONFIG_FLAGS (drive)->test_write = 0;
+       CDROM_CONFIG_FLAGS (drive)->dvd = 0;
+       CDROM_CONFIG_FLAGS (drive)->dvd_r = 0;
+       CDROM_CONFIG_FLAGS (drive)->dvd_ram = 0;
+       CDROM_CONFIG_FLAGS (drive)->no_eject = 1;
+       CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 0;
+       CDROM_CONFIG_FLAGS (drive)->audio_play = 0;
+       CDROM_CONFIG_FLAGS (drive)->close_tray = 1;
+       
+       /* limit transfer size per interrupt. */
+       CDROM_CONFIG_FLAGS (drive)->limit_nframes = 0;
+       if (drive->id != NULL) {
+               /* a testament to the nice quality of Samsung drives... */
+               if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
+                       CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
+               else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
+                       CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
+               /* the 3231 model does not support the SET_CD_SPEED command */
+               else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
+                       cdi->mask |= CDC_SELECT_SPEED;
+       }
+
+#if ! STANDARD_ATAPI
+       /* by default Sanyo 3 CD changer support is turned off and
+           ATAPI Rev 2.2+ standard support for CD changers is used */
+       cdi->sanyo_slot = 0;
+
+       CDROM_CONFIG_FLAGS (drive)->nec260 = 0;
+       CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
+       CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
+       CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
+       CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
+
+       if (drive->id != NULL) {
+               if (strcmp (drive->id->model, "V003S0DS") == 0 &&
+                   drive->id->fw_rev[4] == '1' &&
+                   drive->id->fw_rev[6] <= '2') {
+                       /* Vertos 300.
+                          Some versions of this drive like to talk BCD. */
+                       CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
+                       CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
+                       CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
+                       CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
+               }
+
+               else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
+                   drive->id->fw_rev[4] == '1' &&
+                   drive->id->fw_rev[6] <= '2') {
+                       /* Vertos 600 ESD. */
+                       CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
+               }
+
+               else if (strcmp (drive->id->model,
+                                "NEC CD-ROM DRIVE:260") == 0 &&
+                        strncmp (drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
+                       /* Old NEC260 (not R).
+                          This drive was released before the 1.2 version
+                          of the spec. */
+                       CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
+                       CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
+                       CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
+                       CDROM_CONFIG_FLAGS (drive)->nec260         = 1;
+               }
+
+               else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
+                        strncmp (drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
+                       /* Wearnes */
+                       CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
+                       CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
+               }
+
+                /* Sanyo 3 CD changer uses a non-standard command
+                    for CD changing */
+                 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
+                         (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
+                         (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
+                        /* uses CD in slot 0 when value is set to 3 */
+                        cdi->sanyo_slot = 3;
+                }
+
+
+       }
+#endif /* not STANDARD_ATAPI */
+
+       info->toc               = NULL;
+       info->buffer            = NULL;
+       info->sector_buffered   = 0;
+       info->nsectors_buffered = 0;
+       info->changer_info      = NULL;
+       info->last_block        = 0;
+       info->start_seek        = 0;
+
+       nslots = ide_cdrom_probe_capabilities (drive);
+
+       if (CDROM_CONFIG_FLAGS(drive)->dvd_ram)
+               set_device_ro(MKDEV(HWIF(drive)->major, minor), 0);
+
+       if (ide_cdrom_register (drive, nslots)) {
+               printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
+               info->devinfo.handle = NULL;
+               return 1;
+       }
+       ide_cdrom_add_settings(drive);
+       return 0;
+}
+
+/* Forwarding functions to generic routines. */
+static
+int ide_cdrom_ioctl (ide_drive_t *drive,
+                    struct inode *inode, struct file *file,
+                    unsigned int cmd, unsigned long arg)
+{
+       return cdrom_ioctl (inode, file, cmd, arg);
+}
+
+static
+int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
+{
+       struct cdrom_info *info = drive->driver_data;
+       int rc = -ENOMEM;
+
+       MOD_INC_USE_COUNT;
+       if (info->buffer == NULL)
+               info->buffer = (char *) kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL);
+        if ((info->buffer == NULL) || (rc = cdrom_open(ip, fp))) {
+               drive->usage--;
+               MOD_DEC_USE_COUNT;
+       }
+       return rc;
+}
+
+static
+void ide_cdrom_release (struct inode *inode, struct file *file,
+                       ide_drive_t *drive)
+{
+       cdrom_release (inode, file);
+       MOD_DEC_USE_COUNT;
+}
+
+static
+int ide_cdrom_check_media_change (ide_drive_t *drive)
+{
+       return cdrom_media_changed(MKDEV (HWIF (drive)->major,
+                       (drive->select.b.unit) << PARTN_BITS));
+}
+
+static
+void ide_cdrom_revalidate (ide_drive_t *drive)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct atapi_toc *toc;
+       int minor = drive->select.b.unit << PARTN_BITS;
+       struct request_sense sense;
+
+       cdrom_read_toc(drive, &sense);
+
+       if (!CDROM_STATE_FLAGS(drive)->toc_valid)
+               return;
+
+       toc = info->toc;
+
+       /* for general /dev/cdrom like mounting, one big disc */
+       drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
+       HWIF(drive)->gd->sizes[minor] = toc->capacity * BLOCKS_PER_FRAME;
+
+       /*
+        * reset block size, ide_revalidate_disk incorrectly sets it to
+        * 1024 even for CDROM's
+        */
+       blk_size[HWIF(drive)->major] = HWIF(drive)->gd->sizes;
+#if 0
+       set_blocksize(MKDEV(HWIF(drive)->major, minor), CD_FRAMESIZE);
+#endif
+}
+
+static
+unsigned long ide_cdrom_capacity (ide_drive_t *drive)
+{
+       unsigned long capacity;
+
+       if (cdrom_read_capacity(drive, &capacity, NULL))
+               return 0;
+
+       return capacity * SECTORS_PER_FRAME;
+}
+
+static
+int ide_cdrom_cleanup(ide_drive_t *drive)
+{
+       struct cdrom_info *info = drive->driver_data;
+       struct cdrom_device_info *devinfo = &info->devinfo;
+
+       if (ide_unregister_subdriver (drive))
+               return 1;
+       if (info->buffer != NULL)
+               kfree(info->buffer);
+       if (info->toc != NULL)
+               kfree(info->toc);
+       if (info->changer_info != NULL)
+               kfree(info->changer_info);
+       if (devinfo->handle == drive && unregister_cdrom (devinfo))
+               printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
+       kfree(info);
+       drive->driver_data = NULL;
+       return 0;
+}
+
+static
+int ide_cdrom_reinit (ide_drive_t *drive)
+{
+       return 0;
+}
+
+static ide_driver_t ide_cdrom_driver = {
+       name:                   "ide-cdrom",
+       version:                IDECD_VERSION,
+       media:                  ide_cdrom,
+       busy:                   0,
+       supports_dma:           1,
+       supports_dsc_overlap:   1,
+       cleanup:                ide_cdrom_cleanup,
+       do_request:             ide_do_rw_cdrom,
+       end_request:            NULL,
+       ioctl:                  ide_cdrom_ioctl,
+       open:                   ide_cdrom_open,
+       release:                ide_cdrom_release,
+       media_change:           ide_cdrom_check_media_change,
+       revalidate:             ide_cdrom_revalidate,
+       pre_reset:              NULL,
+       capacity:               ide_cdrom_capacity,
+       special:                NULL,
+#if 0
+       proc:                   NULL,
+       driver_reinit:          ide_cdrom_reinit,
+#endif
+};
+
+int ide_cdrom_init(void);
+static ide_module_t ide_cdrom_module = {
+       IDE_DRIVER_MODULE,
+       ide_cdrom_init,
+       &ide_cdrom_driver,
+       NULL
+};
+
+/* options */
+char *ignore = NULL;
+
+MODULE_PARM(ignore, "s");
+MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
+
+static void __exit ide_cdrom_exit(void)
+{
+       ide_drive_t *drive;
+       int failed = 0;
+
+       while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, &ide_cdrom_driver, failed)) != NULL)
+               if (ide_cdrom_cleanup (drive)) {
+                       printk ("%s: cleanup_module() called while still busy\n", drive->name);
+                       failed++;
+               }
+       ide_unregister_module (&ide_cdrom_module);
+}
+int ide_cdrom_init(void)
+{
+       ide_drive_t *drive;
+       struct cdrom_info *info;
+       int failed = 0;
+
+       MOD_INC_USE_COUNT;
+       while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, NULL, failed++)) != NULL) {
+               /* skip drives that we were told to ignore */
+               if (ignore != NULL) {
+                       if (strstr(ignore, drive->name)) {
+                               printk("ide-cd: ignoring drive %s\n", drive->name);
+                               continue;
+                       }
+               }
+               if (drive->scsi) {
+                       printk("ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
+                       continue;
+               }
+               info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
+               if (info == NULL) {
+                       printk ("%s: Can't allocate a cdrom structure\n", drive->name);
+                       continue;
+               }
+               if (ide_register_subdriver (drive, &ide_cdrom_driver, IDE_SUBDRIVER_VERSION)) {
+                       printk ("%s: Failed to register the driver with ide.c\n", drive->name);
+                       kfree (info);
+                       continue;
+               }
+               memset (info, 0, sizeof (struct cdrom_info));
+               drive->driver_data = info;
+               DRIVER(drive)->busy++;
+               if (ide_cdrom_setup (drive)) {
+                       DRIVER(drive)->busy--;
+                       if (ide_cdrom_cleanup (drive))
+                               printk ("%s: ide_cdrom_cleanup failed in ide_cdrom_init\n", drive->name);
+                       continue;
+               }
+               DRIVER(drive)->busy--;
+               failed--;
+       }
+       ide_register_module(&ide_cdrom_module);
+       MOD_DEC_USE_COUNT;
+       return 0;
+}
+
+module_init(ide_cdrom_init);
+module_exit(ide_cdrom_exit);
+MODULE_LICENSE("GPL");
diff --git a/xen/drivers/ide/ide-cd.h b/xen/drivers/ide/ide-cd.h
new file mode 100644 (file)
index 0000000..636e88b
--- /dev/null
@@ -0,0 +1,746 @@
+/*
+ *  linux/drivers/ide/ide_cd.h
+ *
+ *  Copyright (C) 1996-98  Erik Andersen
+ *  Copyright (C) 1998-2000 Jens Axboe
+ */
+#ifndef _IDE_CD_H
+#define _IDE_CD_H
+
+#include <xeno/cdrom.h>
+#include <asm/byteorder.h>
+
+/* Turn this on to have the driver print out the meanings of the
+   ATAPI error codes.  This will use up additional kernel-space
+   memory, though. */
+
+#ifndef VERBOSE_IDE_CD_ERRORS
+#define VERBOSE_IDE_CD_ERRORS 1
+#endif
+
+
+/* Turning this on will remove code to work around various nonstandard
+   ATAPI implementations.  If you know your drive follows the standard,
+   this will give you a slightly smaller kernel. */
+
+#ifndef STANDARD_ATAPI
+#define STANDARD_ATAPI 0
+#endif
+
+
+/* Turning this on will disable the door-locking functionality.
+   This is apparently needed for supermount. */
+
+#ifndef NO_DOOR_LOCKING
+#define NO_DOOR_LOCKING 0
+#endif
+
+/************************************************************************/
+
+#define SECTOR_BITS            9
+// XXX SMH: already defined in ide.h
+#ifdef SECTOR_SIZE
+#undef SECTOR_SIZE 
+#endif
+#define SECTOR_SIZE            (1 << SECTOR_BITS)
+#define SECTORS_PER_FRAME      (CD_FRAMESIZE >> SECTOR_BITS)
+#define SECTOR_BUFFER_SIZE     (CD_FRAMESIZE * 32)
+#define SECTORS_BUFFER         (SECTOR_BUFFER_SIZE >> SECTOR_BITS)
+#define SECTORS_MAX            (131072 >> SECTOR_BITS)
+
+#define BLOCKS_PER_FRAME       (CD_FRAMESIZE / BLOCK_SIZE)
+
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+
+/* special command codes for strategy routine. */
+#define PACKET_COMMAND        4315
+#define REQUEST_SENSE_COMMAND 4316
+#define RESET_DRIVE_COMMAND   4317
+
+
+/* Configuration flags.  These describe the capabilities of the drive.
+   They generally do not change after initialization, unless we learn
+   more about the drive from stuff failing. */
+struct ide_cd_config_flags {
+       __u8 drq_interrupt      : 1; /* Device sends an interrupt when ready
+                                       for a packet command. */
+       __u8 no_doorlock        : 1; /* Drive cannot lock the door. */
+       __u8 no_eject           : 1; /* Drive cannot eject the disc. */
+       __u8 nec260             : 1; /* Drive is a pre-1.2 NEC 260 drive. */
+       __u8 playmsf_as_bcd     : 1; /* PLAYMSF command takes BCD args. */
+       __u8 tocaddr_as_bcd     : 1; /* TOC addresses are in BCD. */
+       __u8 toctracks_as_bcd   : 1; /* TOC track numbers are in BCD. */
+       __u8 subchan_as_bcd     : 1; /* Subchannel info is in BCD. */
+       __u8 is_changer         : 1; /* Drive is a changer. */
+       __u8 cd_r               : 1; /* Drive can write to CD-R media . */
+       __u8 cd_rw              : 1; /* Drive can write to CD-R/W media . */
+       __u8 dvd                : 1; /* Drive is a DVD-ROM */
+       __u8 dvd_r              : 1; /* Drive can write DVD-R */
+       __u8 dvd_ram            : 1; /* Drive can write DVD-RAM */
+       __u8 test_write         : 1; /* Drive can fake writes */
+       __u8 supp_disc_present  : 1; /* Changer can report exact contents
+                                       of slots. */
+       __u8 limit_nframes      : 1; /* Drive does not provide data in
+                                       multiples of SECTOR_SIZE when more
+                                       than one interrupt is needed. */
+       __u8 seeking            : 1; /* Seeking in progress */
+       __u8 audio_play         : 1; /* can do audio related commands */
+       __u8 close_tray         : 1; /* can close the tray */
+       __u8 writing            : 1; /* pseudo write in progress */
+       __u8 reserved           : 3;
+       byte max_speed;              /* Max speed of the drive */
+};
+#define CDROM_CONFIG_FLAGS(drive) (&(((struct cdrom_info *)(drive->driver_data))->config_flags))
+
+/* State flags.  These give information about the current state of the
+   drive, and will change during normal operation. */
+struct ide_cd_state_flags {
+       __u8 media_changed : 1; /* Driver has noticed a media change. */
+       __u8 toc_valid     : 1; /* Saved TOC information is current. */
+       __u8 door_locked   : 1; /* We think that the drive door is locked. */
+       __u8 writing       : 1; /* the drive is currently writing */
+       __u8 reserved      : 4;
+       byte current_speed;     /* Current speed of the drive */
+};
+
+#define CDROM_STATE_FLAGS(drive) (&(((struct cdrom_info *)(drive->driver_data))->state_flags))
+
+struct packet_command {
+       char *buffer;
+       int buflen;
+       int stat;
+       int quiet;
+       int timeout;
+       struct request_sense *sense;
+       unsigned char c[12];
+};
+
+/* Structure of a MSF cdrom address. */
+struct atapi_msf {
+       byte reserved;
+       byte minute;
+       byte second;
+       byte frame;
+};
+
+/* Space to hold the disk TOC. */
+#define MAX_TRACKS 99
+struct atapi_toc_header {
+       unsigned short toc_length;
+       byte first_track;
+       byte last_track;
+};
+
+struct atapi_toc_entry {
+       byte reserved1;
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 adr     : 4;
+       __u8 control : 4;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 control : 4;
+       __u8 adr     : 4;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+       byte track;
+       byte reserved2;
+       union {
+               unsigned lba;
+               struct atapi_msf msf;
+       } addr;
+};
+
+struct atapi_toc {
+       int    last_session_lba;
+       int    xa_flag;
+       unsigned long capacity;
+       struct atapi_toc_header hdr;
+       struct atapi_toc_entry  ent[MAX_TRACKS+1];
+         /* One extra for the leadout. */
+};
+
+
+/* This structure is annoyingly close to, but not identical with,
+   the cdrom_subchnl structure from cdrom.h. */
+struct atapi_cdrom_subchnl {
+       u_char  acdsc_reserved;
+       u_char  acdsc_audiostatus;
+       u_short acdsc_length;
+       u_char  acdsc_format;
+
+#if defined(__BIG_ENDIAN_BITFIELD)
+       u_char  acdsc_ctrl:     4;
+       u_char  acdsc_adr:      4;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       u_char  acdsc_adr:      4;
+       u_char  acdsc_ctrl:     4;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+       u_char  acdsc_trk;
+       u_char  acdsc_ind;
+       union {
+               struct atapi_msf msf;
+               int     lba;
+       } acdsc_absaddr;
+       union {
+               struct atapi_msf msf;
+               int     lba;
+       } acdsc_reladdr;
+};
+
+
+
+/* This should probably go into cdrom.h along with the other
+ * generic stuff now in the Mt. Fuji spec.
+ */
+struct atapi_capabilities_page {
+       struct mode_page_header header;
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 parameters_saveable : 1;
+       __u8 reserved1           : 1;
+       __u8 page_code           : 6;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 page_code           : 6;
+       __u8 reserved1           : 1;
+       __u8 parameters_saveable : 1;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+       byte     page_length;
+
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved2           : 2;
+       /* Drive supports reading of DVD-RAM discs */
+       __u8 dvd_ram_read        : 1;
+       /* Drive supports reading of DVD-R discs */
+       __u8 dvd_r_read          : 1;
+       /* Drive supports reading of DVD-ROM discs */
+       __u8 dvd_rom             : 1;
+       /* Drive supports reading CD-R discs with addressing method 2 */
+       __u8 method2             : 1; /* reserved in 1.2 */
+       /* Drive can read from CD-R/W (CD-E) discs (orange book, part III) */
+       __u8 cd_rw_read          : 1; /* reserved in 1.2 */
+       /* Drive supports read from CD-R discs (orange book, part II) */
+       __u8 cd_r_read           : 1; /* reserved in 1.2 */
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       /* Drive supports read from CD-R discs (orange book, part II) */
+       __u8 cd_r_read           : 1; /* reserved in 1.2 */
+       /* Drive can read from CD-R/W (CD-E) discs (orange book, part III) */
+       __u8 cd_rw_read          : 1; /* reserved in 1.2 */
+       /* Drive supports reading CD-R discs with addressing method 2 */
+       __u8 method2             : 1;
+       /* Drive supports reading of DVD-ROM discs */
+       __u8 dvd_rom             : 1;
+       /* Drive supports reading of DVD-R discs */
+       __u8 dvd_r_read          : 1;
+       /* Drive supports reading of DVD-RAM discs */
+       __u8 dvd_ram_read        : 1;
+       __u8 reserved2           : 2;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved3           : 2;
+       /* Drive can write DVD-RAM discs */
+       __u8 dvd_ram_write       : 1;
+       /* Drive can write DVD-R discs */
+       __u8 dvd_r_write         : 1;
+       __u8 reserved3a          : 1;
+       /* Drive can fake writes */
+       __u8 test_write          : 1;
+       /* Drive can write to CD-R/W (CD-E) discs (orange book, part III) */
+       __u8 cd_rw_write         : 1; /* reserved in 1.2 */
+       /* Drive supports write to CD-R discs (orange book, part II) */
+       __u8 cd_r_write          : 1; /* reserved in 1.2 */
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       /* Drive can write to CD-R discs (orange book, part II) */
+       __u8 cd_r_write          : 1; /* reserved in 1.2 */
+       /* Drive can write to CD-R/W (CD-E) discs (orange book, part III) */
+       __u8 cd_rw_write         : 1; /* reserved in 1.2 */
+       /* Drive can fake writes */
+       __u8 test_write          : 1;
+       __u8 reserved3a          : 1;
+       /* Drive can write DVD-R discs */
+       __u8 dvd_r_write         : 1;
+       /* Drive can write DVD-RAM discs */
+       __u8 dvd_ram_write       : 1;
+       __u8 reserved3           : 2;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved4           : 1;
+       /* Drive can read multisession discs. */
+       __u8 multisession        : 1;
+       /* Drive can read mode 2, form 2 data. */
+       __u8 mode2_form2         : 1;
+       /* Drive can read mode 2, form 1 (XA) data. */
+       __u8 mode2_form1         : 1;
+       /* Drive supports digital output on port 2. */
+       __u8 digport2            : 1;
+       /* Drive supports digital output on port 1. */
+       __u8 digport1            : 1;
+       /* Drive can deliver a composite audio/video data stream. */
+       __u8 composite           : 1;
+       /* Drive supports audio play operations. */
+       __u8 audio_play          : 1;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       /* Drive supports audio play operations. */
+       __u8 audio_play          : 1;
+       /* Drive can deliver a composite audio/video data stream. */
+       __u8 composite           : 1;
+       /* Drive supports digital output on port 1. */
+       __u8 digport1            : 1;
+       /* Drive supports digital output on port 2. */
+       __u8 digport2            : 1;
+       /* Drive can read mode 2, form 1 (XA) data. */
+       __u8 mode2_form1         : 1;
+       /* Drive can read mode 2, form 2 data. */
+       __u8 mode2_form2         : 1;
+       /* Drive can read multisession discs. */
+       __u8 multisession        : 1;
+       __u8 reserved4           : 1;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved5           : 1;
+       /* Drive can return Media Catalog Number (UPC) info. */
+       __u8 upc                 : 1;
+       /* Drive can return International Standard Recording Code info. */
+       __u8 isrc                : 1;
+       /* Drive supports C2 error pointers. */
+       __u8 c2_pointers         : 1;
+       /* R-W data will be returned deinterleaved and error corrected. */
+       __u8 rw_corr             : 1;
+       /* Subchannel reads can return combined R-W information. */
+       __u8 rw_supported        : 1;
+       /* Drive can continue a read cdda operation from a loss of streaming.*/
+       __u8 cdda_accurate       : 1;
+       /* Drive can read Red Book audio data. */
+       __u8 cdda                : 1;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       /* Drive can read Red Book audio data. */
+       __u8 cdda                : 1;
+       /* Drive can continue a read cdda operation from a loss of streaming.*/
+       __u8 cdda_accurate       : 1;
+       /* Subchannel reads can return combined R-W information. */
+       __u8 rw_supported        : 1;
+       /* R-W data will be returned deinterleaved and error corrected. */
+       __u8 rw_corr             : 1;
+       /* Drive supports C2 error pointers. */
+       __u8 c2_pointers         : 1;
+       /* Drive can return International Standard Recording Code info. */
+       __u8 isrc                : 1;
+       /* Drive can return Media Catalog Number (UPC) info. */
+       __u8 upc                 : 1;
+       __u8 reserved5           : 1;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+#if defined(__BIG_ENDIAN_BITFIELD)
+       /* Drive mechanism types. */
+       mechtype_t mechtype      : 3;
+       __u8 reserved6           : 1;
+       /* Drive can eject a disc or changer cartridge. */
+       __u8 eject               : 1;
+       /* State of prevent/allow jumper. */
+       __u8 prevent_jumper      : 1;
+       /* Present state of door lock. */
+       __u8 lock_state          : 1;
+       /* Drive can lock the door. */
+       __u8 lock                : 1;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+
+       /* Drive can lock the door. */
+       __u8 lock                : 1;
+       /* Present state of door lock. */
+       __u8 lock_state          : 1;
+       /* State of prevent/allow jumper. */
+       __u8 prevent_jumper      : 1;
+       /* Drive can eject a disc or changer cartridge. */
+       __u8 eject               : 1;
+       __u8 reserved6           : 1;
+       /* Drive mechanism types. */
+       mechtype_t mechtype      : 3;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved7           : 4;
+       /* Drive supports software slot selection. */
+       __u8 sss                 : 1;  /* reserved in 1.2 */
+       /* Changer can report exact contents of slots. */
+       __u8 disc_present        : 1;  /* reserved in 1.2 */
+       /* Audio for each channel can be muted independently. */
+       __u8 separate_mute       : 1;
+       /* Audio level for each channel can be controlled independently. */
+       __u8 separate_volume     : 1;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+
+       /* Audio level for each channel can be controlled independently. */
+       __u8 separate_volume     : 1;
+       /* Audio for each channel can be muted independently. */
+       __u8 separate_mute       : 1;
+       /* Changer can report exact contents of slots. */
+       __u8 disc_present        : 1;  /* reserved in 1.2 */
+       /* Drive supports software slot selection. */
+       __u8 sss                 : 1;  /* reserved in 1.2 */
+       __u8 reserved7           : 4;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+       /* Note: the following four fields are returned in big-endian form. */
+       /* Maximum speed (in kB/s). */
+       unsigned short maxspeed;
+       /* Number of discrete volume levels. */
+       unsigned short n_vol_levels;
+       /* Size of cache in drive, in kB. */
+       unsigned short buffer_size;
+       /* Current speed (in kB/s). */
+       unsigned short curspeed;
+       char pad[4];
+};
+
+
+struct atapi_mechstat_header {
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 fault         : 1;
+       __u8 changer_state : 2;
+       __u8 curslot       : 5;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 curslot       : 5;
+       __u8 changer_state : 2;
+       __u8 fault         : 1;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 mech_state    : 3;
+       __u8 door_open     : 1;
+       __u8 reserved1     : 4;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 reserved1     : 4;
+       __u8 door_open     : 1;
+       __u8 mech_state    : 3;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+       byte     curlba[3];
+       byte     nslots;
+       __u8 short slot_tablelen;
+};
+
+
+struct atapi_slot {
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 disc_present : 1;
+       __u8 reserved1    : 6;
+       __u8 change       : 1;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 change       : 1;
+       __u8 reserved1    : 6;
+       __u8 disc_present : 1;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+
+       byte reserved2[3];
+};
+
+struct atapi_changer_info {
+       struct atapi_mechstat_header hdr;
+       struct atapi_slot slots[0];
+};
+
+/* Extra per-device info for cdrom drives. */
+struct cdrom_info {
+
+       /* Buffer for table of contents.  NULL if we haven't allocated
+          a TOC buffer for this device yet. */
+
+       struct atapi_toc *toc;
+
+       unsigned long   sector_buffered;
+       unsigned long   nsectors_buffered;
+       unsigned char   *buffer;
+
+       /* The result of the last successful request sense command
+          on this device. */
+       struct request_sense sense_data;
+
+       struct request request_sense_request;
+       struct packet_command request_sense_pc;
+       int dma;
+       int cmd;
+       unsigned long last_block;
+       unsigned long start_seek;
+       /* Buffer to hold mechanism status and changer slot table. */
+       struct atapi_changer_info *changer_info;
+
+       struct ide_cd_config_flags      config_flags;
+       struct ide_cd_state_flags       state_flags;
+
+        /* Per-device info needed by cdrom.c generic driver. */
+        struct cdrom_device_info devinfo;
+};
+
+/****************************************************************************
+ * Descriptions of ATAPI error codes.
+ */
+
+#define ARY_LEN(a) ((sizeof(a) / sizeof(a[0])))
+
+/* This stuff should be in cdrom.h, since it is now generic... */
+
+/* ATAPI sense keys (from table 140 of ATAPI 2.6) */
+#define NO_SENSE                0x00
+#define RECOVERED_ERROR         0x01
+#define NOT_READY               0x02
+#define MEDIUM_ERROR            0x03
+#define HARDWARE_ERROR          0x04
+#define ILLEGAL_REQUEST         0x05
+#define UNIT_ATTENTION          0x06
+#define DATA_PROTECT            0x07
+#define ABORTED_COMMAND         0x0b
+#define MISCOMPARE              0x0e
+
+
+/* This stuff should be in cdrom.h, since it is now generic... */
+#if VERBOSE_IDE_CD_ERRORS
+
+ /* The generic packet command opcodes for CD/DVD Logical Units,
+ * From Table 57 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */ 
+const struct {
+       unsigned short packet_command;
+       const char * const text;
+} packet_command_texts[] = {
+       { GPCMD_TEST_UNIT_READY, "Test Unit Ready" },
+       { GPCMD_REQUEST_SENSE, "Request Sense" },
+       { GPCMD_FORMAT_UNIT, "Format Unit" },
+       { GPCMD_INQUIRY, "Inquiry" },
+       { GPCMD_START_STOP_UNIT, "Start/Stop Unit" },
+       { GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL, "Prevent/Allow Medium Removal" },
+       { GPCMD_READ_FORMAT_CAPACITIES, "Read Format Capacities" },
+       { GPCMD_READ_CDVD_CAPACITY, "Read Cd/Dvd Capacity" },
+       { GPCMD_READ_10, "Read 10" },
+       { GPCMD_WRITE_10, "Write 10" },
+       { GPCMD_SEEK, "Seek" },
+       { GPCMD_WRITE_AND_VERIFY_10, "Write and Verify 10" },
+       { GPCMD_VERIFY_10, "Verify 10" },
+       { GPCMD_FLUSH_CACHE, "Flush Cache" },
+       { GPCMD_READ_SUBCHANNEL, "Read Subchannel" },
+       { GPCMD_READ_TOC_PMA_ATIP, "Read Table of Contents" },
+       { GPCMD_READ_HEADER, "Read Header" },
+       { GPCMD_PLAY_AUDIO_10, "Play Audio 10" },
+       { GPCMD_GET_CONFIGURATION, "Get Configuration" },
+       { GPCMD_PLAY_AUDIO_MSF, "Play Audio MSF" },
+       { GPCMD_PLAYAUDIO_TI, "Play Audio TrackIndex" },
+       { GPCMD_GET_EVENT_STATUS_NOTIFICATION, "Get Event Status Notification" },
+       { GPCMD_PAUSE_RESUME, "Pause/Resume" },
+       { GPCMD_STOP_PLAY_SCAN, "Stop Play/Scan" },
+       { GPCMD_READ_DISC_INFO, "Read Disc Info" },
+       { GPCMD_READ_TRACK_RZONE_INFO, "Read Track Rzone Info" },
+       { GPCMD_RESERVE_RZONE_TRACK, "Reserve Rzone Track" },
+       { GPCMD_SEND_OPC, "Send OPC" },
+       { GPCMD_MODE_SELECT_10, "Mode Select 10" },
+       { GPCMD_REPAIR_RZONE_TRACK, "Repair Rzone Track" },
+       { GPCMD_MODE_SENSE_10, "Mode Sense 10" },
+       { GPCMD_CLOSE_TRACK, "Close Track" },
+       { GPCMD_BLANK, "Blank" },
+       { GPCMD_SEND_EVENT, "Send Event" },
+       { GPCMD_SEND_KEY, "Send Key" },
+       { GPCMD_REPORT_KEY, "Report Key" },
+       { GPCMD_LOAD_UNLOAD, "Load/Unload" },
+       { GPCMD_SET_READ_AHEAD, "Set Read-ahead" },
+       { GPCMD_READ_12, "Read 12" },
+       { GPCMD_GET_PERFORMANCE, "Get Performance" },
+       { GPCMD_SEND_DVD_STRUCTURE, "Send DVD Structure" },
+       { GPCMD_READ_DVD_STRUCTURE, "Read DVD Structure" },
+       { GPCMD_SET_STREAMING, "Set Streaming" },
+       { GPCMD_READ_CD_MSF, "Read CD MSF" },
+       { GPCMD_SCAN, "Scan" },
+       { GPCMD_SET_SPEED, "Set Speed" },
+       { GPCMD_PLAY_CD, "Play CD" },
+       { GPCMD_MECHANISM_STATUS, "Mechanism Status" },
+       { GPCMD_READ_CD, "Read CD" },
+};
+
+
+
+/* From Table 303 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
+const char * const sense_key_texts[16] = {
+       "No sense data",
+       "Recovered error",
+       "Not ready",
+       "Medium error",
+       "Hardware error",
+       "Illegal request",
+       "Unit attention",
+       "Data protect",
+       "(reserved)",
+       "(reserved)",
+       "(reserved)",
+       "Aborted command",
+       "(reserved)",
+       "(reserved)",
+       "Miscompare",
+       "(reserved)",
+};
+
+/* From Table 304 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
+const struct {
+       unsigned long asc_ascq;
+       const char * const text;
+} sense_data_texts[] = {
+       { 0x000000, "No additional sense information" },
+       { 0x000011, "Play operation in progress" },
+       { 0x000012, "Play operation paused" },
+       { 0x000013, "Play operation successfully completed" },
+       { 0x000014, "Play operation stopped due to error" },
+       { 0x000015, "No current audio status to return" },
+       { 0x010c0a, "Write error - padding blocks added" },
+       { 0x011700, "Recovered data with no error correction applied" },
+       { 0x011701, "Recovered data with retries" },
+       { 0x011702, "Recovered data with positive head offset" },
+       { 0x011703, "Recovered data with negative head offset" },
+       { 0x011704, "Recovered data with retries and/or CIRC applied" },
+       { 0x011705, "Recovered data using previous sector ID" },
+       { 0x011800, "Recovered data with error correction applied" },
+       { 0x011801, "Recovered data with error correction and retries applied"},
+       { 0x011802, "Recovered data - the data was auto-reallocated" },
+       { 0x011803, "Recovered data with CIRC" },
+       { 0x011804, "Recovered data with L-EC" },
+       { 0x015d00, 
+           "Failure prediction threshold exceeded - Predicted logical unit failure" },
+       { 0x015d01, 
+           "Failure prediction threshold exceeded - Predicted media failure" },
+       { 0x015dff, "Failure prediction threshold exceeded - False" },
+       { 0x017301, "Power calibration area almost full" },
+       { 0x020400, "Logical unit not ready - cause not reportable" },
+       /* Following is misspelled in ATAPI 2.6, _and_ in Mt. Fuji */
+       { 0x020401,
+         "Logical unit not ready - in progress [sic] of becoming ready" },
+       { 0x020402, "Logical unit not ready - initializing command required" },
+       { 0x020403, "Logical unit not ready - manual intervention required" },
+       { 0x020404, "Logical unit not ready - format in progress" },
+       { 0x020407, "Logical unit not ready - operation in progress" },
+       { 0x020408, "Logical unit not ready - long write in progress" },
+       { 0x020600, "No reference position found (media may be upside down)" },
+       { 0x023000, "Incompatible medium installed" },
+       { 0x023a00, "Medium not present" },
+       { 0x025300, "Media load or eject failed" },
+       { 0x025700, "Unable to recover table of contents" },
+       { 0x030300, "Peripheral device write fault" },
+       { 0x030301, "No write current" },
+       { 0x030302, "Excessive write errors" },
+       { 0x030c00, "Write error" },
+       { 0x030c01, "Write error - Recovered with auto reallocation" },
+       { 0x030c02, "Write error - auto reallocation failed" },
+       { 0x030c03, "Write error - recommend reassignment" },
+       { 0x030c04, "Compression check miscompare error" },
+       { 0x030c05, "Data expansion occurred during compress" },
+       { 0x030c06, "Block not compressible" },
+       { 0x030c07, "Write error - recovery needed" },
+       { 0x030c08, "Write error - recovery failed" },
+       { 0x030c09, "Write error - loss of streaming" },
+       { 0x031100, "Unrecovered read error" },
+       { 0x031106, "CIRC unrecovered error" },
+       { 0x033101, "Format command failed" },
+       { 0x033200, "No defect spare location available" },
+       { 0x033201, "Defect list update failure" },
+       { 0x035100, "Erase failure" },
+       { 0x037200, "Session fixation error" },
+       { 0x037201, "Session fixation error writin lead-in" },
+       { 0x037202, "Session fixation error writin lead-out" },
+       { 0x037300, "CD control error" },
+       { 0x037302, "Power calibration area is full" },
+       { 0x037303, "Power calibration area error" },
+       { 0x037304, "Program memory area / RMA update failure" },
+       { 0x037305, "Program memory area / RMA is full" },
+       { 0x037306, "Program memory area / RMA is (almost) full" },
+
+       { 0x040200, "No seek complete" },
+       { 0x040300, "Write fault" },
+       { 0x040900, "Track following error" },
+       { 0x040901, "Tracking servo failure" },
+       { 0x040902, "Focus servo failure" },
+       { 0x040903, "Spindle servo failure" },
+       { 0x041500, "Random positioning error" },
+       { 0x041501, "Mechanical positioning or changer error" },
+       { 0x041502, "Positioning error detected by read of medium" },
+       { 0x043c00, "Mechanical positioning or changer error" },
+       { 0x044000, "Diagnostic failure on component (ASCQ)" },
+       { 0x044400, "Internal CD/DVD logical unit failure" },
+       { 0x04b600, "Media load mechanism failed" },
+       { 0x051a00, "Parameter list length error" },
+       { 0x052000, "Invalid command operation code" },
+       { 0x052100, "Logical block address out of range" },
+       { 0x052102, "Invalid address for write" },
+       { 0x052400, "Invalid field in command packet" },
+       { 0x052600, "Invalid field in parameter list" },
+       { 0x052601, "Parameter not supported" },
+       { 0x052602, "Parameter value invalid" },
+       { 0x052700, "Write protected media" },
+       { 0x052c00, "Command sequence error" },
+       { 0x052c03, "Current program area is not empty" },
+       { 0x052c04, "Current program area is empty" },
+       { 0x053001, "Cannot read medium - unknown format" },
+       { 0x053002, "Cannot read medium - incompatible format" },
+       { 0x053900, "Saving parameters not supported" },
+       { 0x054e00, "Overlapped commands attempted" },
+       { 0x055302, "Medium removal prevented" },
+       { 0x055500, "System resource failure" },
+       { 0x056300, "End of user area encountered on this track" },
+       { 0x056400, "Illegal mode for this track or incompatible medium" },
+       { 0x056f00, "Copy protection key exchange failure - Authentication failure" },
+       { 0x056f01, "Copy protection key exchange failure - Key not present" },
+       { 0x056f02, "Copy protection key exchange failure - Key not established" },
+       { 0x056f03, "Read of scrambled sector without authentication" },
+       { 0x056f04, "Media region code is mismatched to logical unit" },
+       { 0x056f05,  "Drive region must be permanent / region reset count error" },
+       { 0x057203, "Session fixation error - incomplete track in session" },
+       { 0x057204, "Empty or partially written reserved track" },
+       { 0x057205, "No more RZONE reservations are allowed" },
+       { 0x05bf00, "Loss of streaming" },
+       { 0x062800, "Not ready to ready transition, medium may have changed" },
+       { 0x062900, "Power on, reset or hardware reset occurred" },
+       { 0x062a00, "Parameters changed" },
+       { 0x062a01, "Mode parameters changed" },
+       { 0x062e00, "Insufficient time for operation" },
+       { 0x063f00, "Logical unit operating conditions have changed" },
+       { 0x063f01, "Microcode has been changed" },
+       { 0x065a00, "Operator request or state change input (unspecified)" },
+       { 0x065a01, "Operator medium removal request" },
+       { 0x0bb900, "Play operation aborted" },
+
+       /* Here we use 0xff for the key (not a valid key) to signify
+        * that these can have _any_ key value associated with them... */
+       { 0xff0401, "Logical unit is in process of becoming ready" },
+       { 0xff0400, "Logical unit not ready, cause not reportable" },
+       { 0xff0402, "Logical unit not ready, initializing command required" },
+       { 0xff0403, "Logical unit not ready, manual intervention required" },
+       { 0xff0500, "Logical unit does not respond to selection" },
+       { 0xff0800, "Logical unit communication failure" },
+       { 0xff0802, "Logical unit communication parity error" },
+       { 0xff0801, "Logical unit communication time-out" },
+       { 0xff2500, "Logical unit not supported" },
+       { 0xff4c00, "Logical unit failed self-configuration" },
+       { 0xff3e00, "Logical unit has not self-configured yet" },
+};
+#endif
+
+
+#endif /* _IDE_CD_H */
diff --git a/xen/include/xeno/cdrom.h b/xen/include/xeno/cdrom.h
new file mode 100644 (file)
index 0000000..ecd4d04
--- /dev/null
@@ -0,0 +1,1067 @@
+/*
+ * -- <linux/cdrom.h>
+ * General header file for linux CD-ROM drivers 
+ * Copyright (C) 1992         David Giller, rafetmad@oxy.edu
+ *               1994, 1995   Eberhard Moenkeberg, emoenke@gwdg.de
+ *               1996         David van Leeuwen, david@tm.tno.nl
+ *               1997, 1998   Erik Andersen, andersee@debian.org
+ *               1998-2000    Jens Axboe, axboe@suse.de
+ */
+#ifndef        _LINUX_CDROM_H
+#define        _LINUX_CDROM_H
+
+#include <asm/byteorder.h>
+
+/*******************************************************
+ * As of Linux 2.1.x, all Linux CD-ROM application programs will use this 
+ * (and only this) include file.  It is my hope to provide Linux with
+ * a uniform interface between software accessing CD-ROMs and the various 
+ * device drivers that actually talk to the drives.  There may still be
+ * 23 different kinds of strange CD-ROM drives, but at least there will 
+ * now be one, and only one, Linux CD-ROM interface.
+ *
+ * Additionally, as of Linux 2.1.x, all Linux application programs 
+ * should use the O_NONBLOCK option when opening a CD-ROM device 
+ * for subsequent ioctl commands.  This allows for neat system errors 
+ * like "No medium found" or "Wrong medium type" upon attempting to 
+ * mount or play an empty slot, mount an audio disc, or play a data disc.
+ * Generally, changing an application program to support O_NONBLOCK
+ * is as easy as the following:
+ *       -    drive = open("/dev/cdrom", O_RDONLY);
+ *       +    drive = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
+ * It is worth the small change.
+ *
+ *  Patches for many common CD programs (provided by David A. van Leeuwen)
+ *  can be found at:  ftp://ftp.gwdg.de/pub/linux/cdrom/drivers/cm206/
+ * 
+ *******************************************************/
+
+/* When a driver supports a certain function, but the cdrom drive we are 
+ * using doesn't, we will return the error EDRIVE_CANT_DO_THIS.  We will 
+ * borrow the "Operation not supported" error from the network folks to 
+ * accomplish this.  Maybe someday we will get a more targeted error code, 
+ * but this will do for now... */
+#define EDRIVE_CANT_DO_THIS  EOPNOTSUPP
+
+/*******************************************************
+ * The CD-ROM IOCTL commands  -- these should be supported by 
+ * all the various cdrom drivers.  For the CD-ROM ioctls, we 
+ * will commandeer byte 0x53, or 'S'.
+ *******************************************************/
+#define CDROMPAUSE             0x5301 /* Pause Audio Operation */ 
+#define CDROMRESUME            0x5302 /* Resume paused Audio Operation */
+#define CDROMPLAYMSF           0x5303 /* Play Audio MSF (struct cdrom_msf) */
+#define CDROMPLAYTRKIND                0x5304 /* Play Audio Track/index 
+                                           (struct cdrom_ti) */
+#define CDROMREADTOCHDR                0x5305 /* Read TOC header 
+                                           (struct cdrom_tochdr) */
+#define CDROMREADTOCENTRY      0x5306 /* Read TOC entry 
+                                           (struct cdrom_tocentry) */
+#define CDROMSTOP              0x5307 /* Stop the cdrom drive */
+#define CDROMSTART             0x5308 /* Start the cdrom drive */
+#define CDROMEJECT             0x5309 /* Ejects the cdrom media */
+#define CDROMVOLCTRL           0x530a /* Control output volume 
+                                           (struct cdrom_volctrl) */
+#define CDROMSUBCHNL           0x530b /* Read subchannel data 
+                                           (struct cdrom_subchnl) */
+#define CDROMREADMODE2         0x530c /* Read CDROM mode 2 data (2336 Bytes) 
+                                           (struct cdrom_read) */
+#define CDROMREADMODE1         0x530d /* Read CDROM mode 1 data (2048 Bytes)
+                                           (struct cdrom_read) */
+#define CDROMREADAUDIO         0x530e /* (struct cdrom_read_audio) */
+#define CDROMEJECT_SW          0x530f /* enable(1)/disable(0) auto-ejecting */
+#define CDROMMULTISESSION      0x5310 /* Obtain the start-of-last-session 
+                                           address of multi session disks 
+                                           (struct cdrom_multisession) */
+#define CDROM_GET_MCN          0x5311 /* Obtain the "Universal Product Code" 
+                                           if available (struct cdrom_mcn) */
+#define CDROM_GET_UPC          CDROM_GET_MCN  /* This one is depricated, 
+                                          but here anyway for compatability */
+#define CDROMRESET             0x5312 /* hard-reset the drive */
+#define CDROMVOLREAD           0x5313 /* Get the drive's volume setting 
+                                          (struct cdrom_volctrl) */
+#define CDROMREADRAW           0x5314  /* read data in raw mode (2352 Bytes)
+                                           (struct cdrom_read) */
+/* 
+ * These ioctls are used only used in aztcd.c and optcd.c
+ */
+#define CDROMREADCOOKED                0x5315  /* read data in cooked mode */
+#define CDROMSEEK              0x5316  /* seek msf address */
+  
+/*
+ * This ioctl is only used by the scsi-cd driver.  
+   It is for playing audio in logical block addressing mode.
+ */
+#define CDROMPLAYBLK           0x5317  /* (struct cdrom_blk) */
+
+/* 
+ * These ioctls are only used in optcd.c
+ */
+#define CDROMREADALL           0x5318  /* read all 2646 bytes */
+
+/* 
+ * These ioctls are (now) only in ide-cd.c for controlling 
+ * drive spindown time.  They should be implemented in the
+ * Uniform driver, via generic packet commands, GPCMD_MODE_SELECT_10,
+ * GPCMD_MODE_SENSE_10 and the GPMODE_POWER_PAGE...
+ *  -Erik
+ */
+#define CDROMGETSPINDOWN        0x531d
+#define CDROMSETSPINDOWN        0x531e
+
+/* 
+ * These ioctls are implemented through the uniform CD-ROM driver
+ * They _will_ be adopted by all CD-ROM drivers, when all the CD-ROM
+ * drivers are eventually ported to the uniform CD-ROM driver interface.
+ */
+#define CDROMCLOSETRAY         0x5319  /* pendant of CDROMEJECT */
+#define CDROM_SET_OPTIONS      0x5320  /* Set behavior options */
+#define CDROM_CLEAR_OPTIONS    0x5321  /* Clear behavior options */
+#define CDROM_SELECT_SPEED     0x5322  /* Set the CD-ROM speed */
+#define CDROM_SELECT_DISC      0x5323  /* Select disc (for juke-boxes) */
+#define CDROM_MEDIA_CHANGED    0x5325  /* Check is media changed  */
+#define CDROM_DRIVE_STATUS     0x5326  /* Get tray position, etc. */
+#define CDROM_DISC_STATUS      0x5327  /* Get disc type, etc. */
+#define CDROM_CHANGER_NSLOTS    0x5328  /* Get number of slots */
+#define CDROM_LOCKDOOR         0x5329  /* lock or unlock door */
+#define CDROM_DEBUG            0x5330  /* Turn debug messages on/off */
+#define CDROM_GET_CAPABILITY   0x5331  /* get capabilities */
+
+/* Note that scsi/scsi_ioctl.h also uses 0x5382 - 0x5386.
+ * Future CDROM ioctls should be kept below 0x537F
+ */
+
+/* This ioctl is only used by sbpcd at the moment */
+#define CDROMAUDIOBUFSIZ        0x5382 /* set the audio buffer size */
+                                       /* conflict with SCSI_IOCTL_GET_IDLUN */
+
+/* DVD-ROM Specific ioctls */
+#define DVD_READ_STRUCT                0x5390  /* Read structure */
+#define DVD_WRITE_STRUCT       0x5391  /* Write structure */
+#define DVD_AUTH               0x5392  /* Authentication */
+
+#define CDROM_SEND_PACKET      0x5393  /* send a packet to the drive */
+#define CDROM_NEXT_WRITABLE    0x5394  /* get next writable block */
+#define CDROM_LAST_WRITTEN     0x5395  /* get last block written on disc */
+
+/*******************************************************
+ * CDROM IOCTL structures
+ *******************************************************/
+
+/* Address in MSF format */
+struct cdrom_msf0              
+{
+       __u8    minute;
+       __u8    second;
+       __u8    frame;
+};
+
+/* Address in either MSF or logical format */
+union cdrom_addr               
+{
+       struct cdrom_msf0       msf;
+       int                     lba;
+};
+
+/* This struct is used by the CDROMPLAYMSF ioctl */ 
+struct cdrom_msf 
+{
+       __u8    cdmsf_min0;     /* start minute */
+       __u8    cdmsf_sec0;     /* start second */
+       __u8    cdmsf_frame0;   /* start frame */
+       __u8    cdmsf_min1;     /* end minute */
+       __u8    cdmsf_sec1;     /* end second */
+       __u8    cdmsf_frame1;   /* end frame */
+};
+
+/* This struct is used by the CDROMPLAYTRKIND ioctl */
+struct cdrom_ti 
+{
+       __u8    cdti_trk0;      /* start track */
+       __u8    cdti_ind0;      /* start index */
+       __u8    cdti_trk1;      /* end track */
+       __u8    cdti_ind1;      /* end index */
+};
+
+/* This struct is used by the CDROMREADTOCHDR ioctl */
+struct cdrom_tochdr    
+{
+       __u8    cdth_trk0;      /* start track */
+       __u8    cdth_trk1;      /* end track */
+};
+
+/* This struct is used by the CDROMVOLCTRL and CDROMVOLREAD ioctls */
+struct cdrom_volctrl
+{
+       __u8    channel0;
+       __u8    channel1;
+       __u8    channel2;
+       __u8    channel3;
+};
+
+/* This struct is used by the CDROMSUBCHNL ioctl */
+struct cdrom_subchnl 
+{
+       __u8    cdsc_format;
+       __u8    cdsc_audiostatus;
+       __u8    cdsc_adr:       4;
+       __u8    cdsc_ctrl:      4;
+       __u8    cdsc_trk;
+       __u8    cdsc_ind;
+       union cdrom_addr cdsc_absaddr;
+       union cdrom_addr cdsc_reladdr;
+};
+
+
+/* This struct is used by the CDROMREADTOCENTRY ioctl */
+struct cdrom_tocentry 
+{
+       __u8    cdte_track;
+       __u8    cdte_adr        :4;
+       __u8    cdte_ctrl       :4;
+       __u8    cdte_format;
+       union cdrom_addr cdte_addr;
+       __u8    cdte_datamode;
+};
+
+/* This struct is used by the CDROMREADMODE1, and CDROMREADMODE2 ioctls */
+struct cdrom_read      
+{
+       int     cdread_lba;
+       char    *cdread_bufaddr;
+       int     cdread_buflen;
+};
+
+/* This struct is used by the CDROMREADAUDIO ioctl */
+struct cdrom_read_audio
+{
+       union cdrom_addr addr; /* frame address */
+       __u8 addr_format;    /* CDROM_LBA or CDROM_MSF */
+       int nframes;           /* number of 2352-byte-frames to read at once */
+       __u8 *buf;           /* frame buffer (size: nframes*2352 bytes) */
+};
+
+/* This struct is used with the CDROMMULTISESSION ioctl */
+struct cdrom_multisession
+{
+       union cdrom_addr addr; /* frame address: start-of-last-session 
+                                  (not the new "frame 16"!).  Only valid
+                                  if the "xa_flag" is true. */
+       __u8 xa_flag;        /* 1: "is XA disk" */
+       __u8 addr_format;    /* CDROM_LBA or CDROM_MSF */
+};
+
+/* This struct is used with the CDROM_GET_MCN ioctl.  
+ * Very few audio discs actually have Universal Product Code information, 
+ * which should just be the Medium Catalog Number on the box.  Also note 
+ * that the way the codeis written on CD is _not_ uniform across all discs!
+ */  
+struct cdrom_mcn 
+{
+  __u8 medium_catalog_number[14]; /* 13 ASCII digits, null-terminated */
+};
+
+/* This is used by the CDROMPLAYBLK ioctl */
+struct cdrom_blk 
+{
+       unsigned from;
+       unsigned short len;
+};
+
+#define CDROM_PACKET_SIZE      12
+
+#define CGC_DATA_UNKNOWN       0
+#define CGC_DATA_WRITE         1
+#define CGC_DATA_READ          2
+#define CGC_DATA_NONE          3
+
+/* for CDROM_PACKET_COMMAND ioctl */
+struct cdrom_generic_command
+{
+       unsigned char           cmd[CDROM_PACKET_SIZE];
+       unsigned char           *buffer;
+       unsigned int            buflen;
+       int                     stat;
+       struct request_sense    *sense;
+       unsigned char           data_direction;
+       int                     quiet;
+       int                     timeout;
+       void                    *reserved[1];
+};
+
+
+/*
+ * A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336, 
+ * 2340, or 2352 bytes long.  
+
+*         Sector types of the standard CD-ROM data formats:
+ *
+ * format   sector type               user data size (bytes)
+ * -----------------------------------------------------------------------------
+ *   1     (Red Book)    CD-DA          2352    (CD_FRAMESIZE_RAW)
+ *   2     (Yellow Book) Mode1 Form1    2048    (CD_FRAMESIZE)
+ *   3     (Yellow Book) Mode1 Form2    2336    (CD_FRAMESIZE_RAW0)
+ *   4     (Green Book)  Mode2 Form1    2048    (CD_FRAMESIZE)
+ *   5     (Green Book)  Mode2 Form2    2328    (2324+4 spare bytes)
+ *
+ *
+ *       The layout of the standard CD-ROM data formats:
+ * -----------------------------------------------------------------------------
+ * - audio (red):                  | audio_sample_bytes |
+ *                                 |        2352        |
+ *
+ * - data (yellow, mode1):         | sync - head - data - EDC - zero - ECC |
+ *                                 |  12  -   4  - 2048 -  4  -   8  - 276 |
+ *
+ * - data (yellow, mode2):         | sync - head - data |
+ *                                 |  12  -   4  - 2336 |
+ *
+ * - XA data (green, mode2 form1): | sync - head - sub - data - EDC - ECC |
+ *                                 |  12  -   4  -  8  - 2048 -  4  - 276 |
+ *
+ * - XA data (green, mode2 form2): | sync - head - sub - data - Spare |
+ *                                 |  12  -   4  -  8  - 2324 -  4    |
+ *
+ */
+
+/* Some generally useful CD-ROM information -- mostly based on the above */
+#define CD_MINS              74 /* max. minutes per CD, not really a limit */
+#define CD_SECS              60 /* seconds per minute */
+#define CD_FRAMES            75 /* frames per second */
+#define CD_SYNC_SIZE         12 /* 12 sync bytes per raw data frame */
+#define CD_MSF_OFFSET       150 /* MSF numbering offset of first frame */
+#define CD_CHUNK_SIZE        24 /* lowest-level "data bytes piece" */
+#define CD_NUM_OF_CHUNKS     98 /* chunks per frame */
+#define CD_FRAMESIZE_SUB     96 /* subchannel data "frame" size */
+#define CD_HEAD_SIZE          4 /* header (address) bytes per raw data frame */
+#define CD_SUBHEAD_SIZE       8 /* subheader bytes per raw XA data frame */
+#define CD_EDC_SIZE           4 /* bytes EDC per most raw data frame types */
+#define CD_ZERO_SIZE          8 /* bytes zero per yellow book mode 1 frame */
+#define CD_ECC_SIZE         276 /* bytes ECC per most raw data frame types */
+#define CD_FRAMESIZE       2048 /* bytes per frame, "cooked" mode */
+#define CD_FRAMESIZE_RAW   2352 /* bytes per frame, "raw" mode */
+#define CD_FRAMESIZE_RAWER 2646 /* The maximum possible returned bytes */ 
+/* most drives don't deliver everything: */
+#define CD_FRAMESIZE_RAW1 (CD_FRAMESIZE_RAW-CD_SYNC_SIZE) /*2340*/
+#define CD_FRAMESIZE_RAW0 (CD_FRAMESIZE_RAW-CD_SYNC_SIZE-CD_HEAD_SIZE) /*2336*/
+
+#define CD_XA_HEAD        (CD_HEAD_SIZE+CD_SUBHEAD_SIZE) /* "before data" part of raw XA frame */
+#define CD_XA_TAIL        (CD_EDC_SIZE+CD_ECC_SIZE) /* "after data" part of raw XA frame */
+#define CD_XA_SYNC_HEAD   (CD_SYNC_SIZE+CD_XA_HEAD) /* sync bytes + header of XA frame */
+
+/* CD-ROM address types (cdrom_tocentry.cdte_format) */
+#define        CDROM_LBA 0x01 /* "logical block": first frame is #0 */
+#define        CDROM_MSF 0x02 /* "minute-second-frame": binary, not bcd here! */
+
+/* bit to tell whether track is data or audio (cdrom_tocentry.cdte_ctrl) */
+#define        CDROM_DATA_TRACK        0x04
+
+/* The leadout track is always 0xAA, regardless of # of tracks on disc */
+#define        CDROM_LEADOUT           0xAA
+
+/* audio states (from SCSI-2, but seen with other drives, too) */
+#define        CDROM_AUDIO_INVALID     0x00    /* audio status not supported */
+#define        CDROM_AUDIO_PLAY        0x11    /* audio play operation in progress */
+#define        CDROM_AUDIO_PAUSED      0x12    /* audio play operation paused */
+#define        CDROM_AUDIO_COMPLETED   0x13    /* audio play successfully completed */
+#define        CDROM_AUDIO_ERROR       0x14    /* audio play stopped due to error */
+#define        CDROM_AUDIO_NO_STATUS   0x15    /* no current audio status to return */
+
+/* capability flags used with the uniform CD-ROM driver */ 
+#define CDC_CLOSE_TRAY         0x1     /* caddy systems _can't_ close */
+#define CDC_OPEN_TRAY          0x2     /* but _can_ eject.  */
+#define CDC_LOCK               0x4     /* disable manual eject */
+#define CDC_SELECT_SPEED       0x8     /* programmable speed */
+#define CDC_SELECT_DISC                0x10    /* select disc from juke-box */
+#define CDC_MULTI_SESSION      0x20    /* read sessions>1 */
+#define CDC_MCN                        0x40    /* Medium Catalog Number */
+#define CDC_MEDIA_CHANGED      0x80    /* media changed */
+#define CDC_PLAY_AUDIO         0x100   /* audio functions */
+#define CDC_RESET               0x200   /* hard reset device */
+#define CDC_IOCTLS              0x400   /* driver has non-standard ioctls */
+#define CDC_DRIVE_STATUS        0x800   /* driver implements drive status */
+#define CDC_GENERIC_PACKET     0x1000  /* driver implements generic packets */
+#define CDC_CD_R               0x2000  /* drive is a CD-R */
+#define CDC_CD_RW              0x4000  /* drive is a CD-RW */
+#define CDC_DVD                        0x8000  /* drive is a DVD */
+#define CDC_DVD_R              0x10000 /* drive can write DVD-R */
+#define CDC_DVD_RAM            0x20000 /* drive can write DVD-RAM */
+
+/* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
+#define CDS_NO_INFO            0       /* if not implemented */
+#define CDS_NO_DISC            1
+#define CDS_TRAY_OPEN          2
+#define CDS_DRIVE_NOT_READY    3
+#define CDS_DISC_OK            4
+
+/* return values for the CDROM_DISC_STATUS ioctl */
+/* can also return CDS_NO_[INFO|DISC], from above */
+#define CDS_AUDIO              100
+#define CDS_DATA_1             101
+#define CDS_DATA_2             102
+#define CDS_XA_2_1             103
+#define CDS_XA_2_2             104
+#define CDS_MIXED              105
+
+/* User-configurable behavior options for the uniform CD-ROM driver */
+#define CDO_AUTO_CLOSE         0x1     /* close tray on first open() */
+#define CDO_AUTO_EJECT         0x2     /* open tray on last release() */
+#define CDO_USE_FFLAGS         0x4     /* use O_NONBLOCK information on open */
+#define CDO_LOCK               0x8     /* lock tray on open files */
+#define CDO_CHECK_TYPE         0x10    /* check type on open for data */
+
+/* Special codes used when specifying changer slots. */
+#define CDSL_NONE              ((int) (~0U>>1)-1)
+#define CDSL_CURRENT           ((int) (~0U>>1))
+
+/* For partition based multisession access. IDE can handle 64 partitions
+ * per drive - SCSI CD-ROM's use minors to differentiate between the
+ * various drives, so we can't do multisessions the same way there.
+ * Use the -o session=x option to mount on them.
+ */
+#define CD_PART_MAX            64
+#define CD_PART_MASK           (CD_PART_MAX - 1)
+
+/*********************************************************************
+ * Generic Packet commands, MMC commands, and such
+ *********************************************************************/
+
+ /* The generic packet command opcodes for CD/DVD Logical Units,
+ * From Table 57 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
+#define GPCMD_BLANK                        0xa1
+#define GPCMD_CLOSE_TRACK                  0x5b
+#define GPCMD_FLUSH_CACHE                  0x35
+#define GPCMD_FORMAT_UNIT                  0x04
+#define GPCMD_GET_CONFIGURATION                    0x46
+#define GPCMD_GET_EVENT_STATUS_NOTIFICATION 0x4a
+#define GPCMD_GET_PERFORMANCE              0xac
+#define GPCMD_INQUIRY                      0x12
+#define GPCMD_LOAD_UNLOAD                  0xa6
+#define GPCMD_MECHANISM_STATUS             0xbd
+#define GPCMD_MODE_SELECT_10               0x55
+#define GPCMD_MODE_SENSE_10                0x5a
+#define GPCMD_PAUSE_RESUME                 0x4b
+#define GPCMD_PLAY_AUDIO_10                0x45
+#define GPCMD_PLAY_AUDIO_MSF               0x47
+#define GPCMD_PLAY_AUDIO_TI                0x48
+#define GPCMD_PLAY_CD                      0xbc
+#define GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL  0x1e
+#define GPCMD_READ_10                      0x28
+#define GPCMD_READ_12                      0xa8
+#define GPCMD_READ_CDVD_CAPACITY           0x25
+#define GPCMD_READ_CD                      0xbe
+#define GPCMD_READ_CD_MSF                  0xb9
+#define GPCMD_READ_DISC_INFO               0x51
+#define GPCMD_READ_DVD_STRUCTURE           0xad
+#define GPCMD_READ_FORMAT_CAPACITIES       0x23
+#define GPCMD_READ_HEADER                  0x44
+#define GPCMD_READ_TRACK_RZONE_INFO        0x52
+#define GPCMD_READ_SUBCHANNEL              0x42
+#define GPCMD_READ_TOC_PMA_ATIP                    0x43
+#define GPCMD_REPAIR_RZONE_TRACK           0x58
+#define GPCMD_REPORT_KEY                   0xa4
+#define GPCMD_REQUEST_SENSE                0x03
+#define GPCMD_RESERVE_RZONE_TRACK          0x53
+#define GPCMD_SCAN                         0xba
+#define GPCMD_SEEK                         0x2b
+#define GPCMD_SEND_DVD_STRUCTURE           0xad
+#define GPCMD_SEND_EVENT                   0xa2
+#define GPCMD_SEND_KEY                     0xa3
+#define GPCMD_SEND_OPC                     0x54
+#define GPCMD_SET_READ_AHEAD               0xa7
+#define GPCMD_SET_STREAMING                0xb6
+#define GPCMD_START_STOP_UNIT              0x1b
+#define GPCMD_STOP_PLAY_SCAN               0x4e
+#define GPCMD_TEST_UNIT_READY              0x00
+#define GPCMD_VERIFY_10                            0x2f
+#define GPCMD_WRITE_10                     0x2a
+#define GPCMD_WRITE_AND_VERIFY_10          0x2e
+/* This is listed as optional in ATAPI 2.6, but is (curiously) 
+ * missing from Mt. Fuji, Table 57.  It _is_ mentioned in Mt. Fuji
+ * Table 377 as an MMC command for SCSi devices though...  Most ATAPI
+ * drives support it. */
+#define GPCMD_SET_SPEED                            0xbb
+/* This seems to be a SCSI specific CD-ROM opcode 
+ * to play data at track/index */
+#define GPCMD_PLAYAUDIO_TI                 0x48
+/*
+ * From MS Media Status Notification Support Specification. For
+ * older drives only.
+ */
+#define GPCMD_GET_MEDIA_STATUS             0xda
+
+/* Mode page codes for mode sense/set */
+#define GPMODE_R_W_ERROR_PAGE          0x01
+#define GPMODE_WRITE_PARMS_PAGE                0x05
+#define GPMODE_AUDIO_CTL_PAGE          0x0e
+#define GPMODE_POWER_PAGE              0x1a
+#define GPMODE_FAULT_FAIL_PAGE         0x1c
+#define GPMODE_TO_PROTECT_PAGE         0x1d
+#define GPMODE_CAPABILITIES_PAGE       0x2a
+#define GPMODE_ALL_PAGES               0x3f
+/* Not in Mt. Fuji, but in ATAPI 2.6 -- depricated now in favor
+ * of MODE_SENSE_POWER_PAGE */
+#define GPMODE_CDROM_PAGE              0x0d
+
+
+
+/* DVD struct types */
+#define DVD_STRUCT_PHYSICAL    0x00
+#define DVD_STRUCT_COPYRIGHT   0x01
+#define DVD_STRUCT_DISCKEY     0x02
+#define DVD_STRUCT_BCA         0x03
+#define DVD_STRUCT_MANUFACT    0x04
+
+struct dvd_layer {
+       __u8 book_version       : 4;
+       __u8 book_type          : 4;
+       __u8 min_rate           : 4;
+       __u8 disc_size          : 4;
+       __u8 layer_type         : 4;
+       __u8 track_path         : 1;
+       __u8 nlayers            : 2;
+       __u8 track_density      : 4;
+       __u8 linear_density     : 4;
+       __u8 bca                : 1;
+       __u32 start_sector;
+       __u32 end_sector;
+       __u32 end_sector_l0;
+};
+
+#define DVD_LAYERS     4
+
+struct dvd_physical {
+       __u8 type;
+       __u8 layer_num;
+       struct dvd_layer layer[DVD_LAYERS];
+};
+
+struct dvd_copyright {
+       __u8 type;
+
+       __u8 layer_num;
+       __u8 cpst;
+       __u8 rmi;
+};
+
+struct dvd_disckey {
+       __u8 type;
+
+       unsigned agid           : 2;
+       __u8 value[2048];
+};
+
+struct dvd_bca {
+       __u8 type;
+
+       int len;
+       __u8 value[188];
+};
+
+struct dvd_manufact {
+       __u8 type;
+
+       __u8 layer_num;
+       int len;
+       __u8 value[2048];
+};
+
+typedef union {
+       __u8 type;
+
+       struct dvd_physical     physical;
+       struct dvd_copyright    copyright;
+       struct dvd_disckey      disckey;
+       struct dvd_bca          bca;
+       struct dvd_manufact     manufact;
+} dvd_struct;
+
+/*
+ * DVD authentication ioctl
+ */
+
+/* Authentication states */
+#define DVD_LU_SEND_AGID       0
+#define DVD_HOST_SEND_CHALLENGE        1
+#define DVD_LU_SEND_KEY1       2
+#define DVD_LU_SEND_CHALLENGE  3
+#define DVD_HOST_SEND_KEY2     4
+
+/* Termination states */
+#define DVD_AUTH_ESTABLISHED   5
+#define DVD_AUTH_FAILURE       6
+
+/* Other functions */
+#define DVD_LU_SEND_TITLE_KEY  7
+#define DVD_LU_SEND_ASF                8
+#define DVD_INVALIDATE_AGID    9
+#define DVD_LU_SEND_RPC_STATE  10
+#define DVD_HOST_SEND_RPC_STATE        11
+
+/* State data */
+typedef __u8 dvd_key[5];               /* 40-bit value, MSB is first elem. */
+typedef __u8 dvd_challenge[10];        /* 80-bit value, MSB is first elem. */
+
+struct dvd_lu_send_agid {
+       __u8 type;
+       unsigned agid           : 2;
+};
+
+struct dvd_host_send_challenge {
+       __u8 type;
+       unsigned agid           : 2;
+
+       dvd_challenge chal;
+};
+
+struct dvd_send_key {
+       __u8 type;
+       unsigned agid           : 2;
+
+       dvd_key key;
+};
+
+struct dvd_lu_send_challenge {
+       __u8 type;
+       unsigned agid           : 2;
+
+       dvd_challenge chal;
+};
+
+#define DVD_CPM_NO_COPYRIGHT   0
+#define DVD_CPM_COPYRIGHTED    1
+
+#define DVD_CP_SEC_NONE                0
+#define DVD_CP_SEC_EXIST       1
+
+#define DVD_CGMS_UNRESTRICTED  0
+#define DVD_CGMS_SINGLE                2
+#define DVD_CGMS_RESTRICTED    3
+
+struct dvd_lu_send_title_key {
+       __u8 type;
+       unsigned agid           : 2;
+
+       dvd_key title_key;
+       int lba;
+       unsigned cpm            : 1;
+       unsigned cp_sec         : 1;
+       unsigned cgms           : 2;
+};
+
+struct dvd_lu_send_asf {
+       __u8 type;
+       unsigned agid           : 2;
+
+       unsigned asf            : 1;
+};
+
+struct dvd_host_send_rpcstate {
+       __u8 type;
+       __u8 pdrc;
+};
+
+struct dvd_lu_send_rpcstate {
+       __u8 type               : 2;
+       __u8 vra                : 3;
+       __u8 ucca               : 3;
+       __u8 region_mask;
+       __u8 rpc_scheme;
+};
+
+typedef union {
+       __u8 type;
+
+       struct dvd_lu_send_agid         lsa;
+       struct dvd_host_send_challenge  hsc;
+       struct dvd_send_key             lsk;
+       struct dvd_lu_send_challenge    lsc;
+       struct dvd_send_key             hsk;
+       struct dvd_lu_send_title_key    lstk;
+       struct dvd_lu_send_asf          lsasf;
+       struct dvd_host_send_rpcstate   hrpcs;
+       struct dvd_lu_send_rpcstate     lrpcs;
+} dvd_authinfo;
+
+struct request_sense {
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 valid              : 1;
+       __u8 error_code         : 7;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 error_code         : 7;
+       __u8 valid              : 1;
+#endif
+       __u8 segment_number;
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved1          : 2;
+       __u8 ili                : 1;
+       __u8 reserved2          : 1;
+       __u8 sense_key          : 4;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 sense_key          : 4;
+       __u8 reserved2          : 1;
+       __u8 ili                : 1;
+       __u8 reserved1          : 2;
+#endif
+       __u8 information[4];
+       __u8 add_sense_len;
+       __u8 command_info[4];
+       __u8 asc;
+       __u8 ascq;
+       __u8 fruc;
+       __u8 sks[3];
+       __u8 asb[46];
+};
+
+#ifdef __KERNEL__
+#if 0
+#include <linux/devfs_fs_kernel.h>
+#else
+#include <xeno/kdev_t.h>
+#endif
+
+struct cdrom_write_settings {
+       unsigned char fpacket;          /* fixed/variable packets */
+       unsigned long packet_size;      /* write out this number of packets */
+       unsigned long nwa;              /* next writeable address */
+       unsigned char writeable;        /* cdrom is writeable */
+};
+
+/* Uniform cdrom data structures for cdrom.c */
+struct cdrom_device_info {
+       struct cdrom_device_ops  *ops;  /* link to device_ops */
+       struct cdrom_device_info *next; /* next device_info for this major */
+       void *handle;                   /* driver-dependent data */
+#if 0
+       devfs_handle_t de;              /* real driver should create this  */
+#endif
+       int number;                     /* generic driver updates this  */
+/* specifications */
+        kdev_t dev;                    /* device number */
+       int mask;                       /* mask of capability: disables them */
+       int speed;                      /* maximum speed for reading data */
+       int capacity;                   /* number of discs in jukebox */
+/* device-related storage */
+       int options             : 30;   /* options flags */
+       unsigned mc_flags       : 2;    /* media change buffer flags */
+       int use_count;                  /* number of times device opened */
+       char name[20];                  /* name of the device type */
+/* per-device flags */
+        __u8 sanyo_slot                : 2;    /* Sanyo 3 CD changer support */
+        __u8 reserved          : 6;    /* not used yet */
+       struct cdrom_write_settings write;
+};
+
+struct cdrom_device_ops {
+/* routines */
+       int (*open) (struct cdrom_device_info *, int);
+       void (*release) (struct cdrom_device_info *);
+       int (*drive_status) (struct cdrom_device_info *, int);
+       int (*media_changed) (struct cdrom_device_info *, int);
+       int (*tray_move) (struct cdrom_device_info *, int);
+       int (*lock_door) (struct cdrom_device_info *, int);
+       int (*select_speed) (struct cdrom_device_info *, int);
+       int (*select_disc) (struct cdrom_device_info *, int);
+       int (*get_last_session) (struct cdrom_device_info *,
+                                struct cdrom_multisession *);
+       int (*get_mcn) (struct cdrom_device_info *,
+                       struct cdrom_mcn *);
+       /* hard reset device */
+       int (*reset) (struct cdrom_device_info *);
+       /* play stuff */
+       int (*audio_ioctl) (struct cdrom_device_info *,unsigned int, void *);
+       /* dev-specific */
+       int (*dev_ioctl) (struct cdrom_device_info *,
+                         unsigned int, unsigned long);
+/* driver specifications */
+       const int capability;   /* capability flags */
+       int n_minors;           /* number of active minor devices */
+       /* handle uniform packets for scsi type devices (scsi,atapi) */
+       int (*generic_packet) (struct cdrom_device_info *,
+                              struct cdrom_generic_command *);
+};
+
+/* the general block_device operations structure: */
+#include <xeno/blkdev.h> /* XXX SMH: pull in wafer thin file and inode defns */
+extern int cdrom_open(struct inode *, struct file *);
+extern int cdrom_release(struct inode *, struct file *);
+extern int cdrom_ioctl(struct inode *, struct file *, unsigned, unsigned long);
+extern int cdrom_media_changed(kdev_t);
+
+extern int register_cdrom(struct cdrom_device_info *cdi);
+extern int unregister_cdrom(struct cdrom_device_info *cdi);
+
+#if 0
+static inline void devfs_plain_cdrom(struct cdrom_device_info *cdi,
+                               struct block_device_operations *ops)
+{
+       char vname[23];
+
+       sprintf (vname, "cdroms/cdrom%d", cdi->number);
+       cdi->de = devfs_register (NULL, vname, DEVFS_FL_DEFAULT,
+                                   MAJOR (cdi->dev), MINOR (cdi->dev),
+                                   S_IFBLK | S_IRUGO | S_IWUGO,
+                                   ops, NULL);
+}
+#endif
+
+typedef struct {
+    int data;
+    int audio;
+    int cdi;
+    int xa;
+    long error;
+} tracktype;
+
+extern void cdrom_count_tracks(struct cdrom_device_info *cdi,tracktype* tracks);
+extern int cdrom_get_next_writable(kdev_t dev, long *next_writable);
+extern int cdrom_get_last_written(kdev_t dev, long *last_written);
+extern int cdrom_number_of_slots(struct cdrom_device_info *cdi);
+extern int cdrom_select_disc(struct cdrom_device_info *cdi, int slot);
+extern int cdrom_mode_select(struct cdrom_device_info *cdi,
+                            struct cdrom_generic_command *cgc);
+extern int cdrom_mode_sense(struct cdrom_device_info *cdi,
+                           struct cdrom_generic_command *cgc,
+                           int page_code, int page_control);
+extern void init_cdrom_command(struct cdrom_generic_command *cgc,
+                              void *buffer, int len, int type);
+extern struct cdrom_device_info *cdrom_find_device(kdev_t dev);
+
+typedef struct {
+       __u16 disc_information_length;
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved1                  : 3;
+        __u8 erasable                  : 1;
+        __u8 border_status             : 2;
+        __u8 disc_status               : 2;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+        __u8 disc_status               : 2;
+        __u8 border_status             : 2;
+        __u8 erasable                  : 1;
+       __u8 reserved1                  : 3;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+       __u8 n_first_track;
+       __u8 n_sessions_lsb;
+       __u8 first_track_lsb;
+       __u8 last_track_lsb;
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 did_v                      : 1;
+        __u8 dbc_v                     : 1;
+        __u8 uru                       : 1;
+        __u8 reserved2                 : 5;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+        __u8 reserved2                 : 5;
+        __u8 uru                       : 1;
+        __u8 dbc_v                     : 1;
+       __u8 did_v                      : 1;
+#endif
+       __u8 disc_type;
+       __u8 n_sessions_msb;
+       __u8 first_track_msb;
+       __u8 last_track_msb;
+       __u32 disc_id;
+       __u32 lead_in;
+       __u32 lead_out;
+       __u8 disc_bar_code[8];
+       __u8 reserved3;
+       __u8 n_opc;
+} disc_information;
+
+typedef struct {
+       __u16 track_information_length;
+       __u8 track_lsb;
+       __u8 session_lsb;
+       __u8 reserved1;
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved2                  : 2;
+        __u8 damage                    : 1;
+        __u8 copy                      : 1;
+        __u8 track_mode                        : 4;
+       __u8 rt                         : 1;
+       __u8 blank                      : 1;
+       __u8 packet                     : 1;
+       __u8 fp                         : 1;
+       __u8 data_mode                  : 4;
+       __u8 reserved3                  : 6;
+       __u8 lra_v                      : 1;
+       __u8 nwa_v                      : 1;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+        __u8 track_mode                        : 4;
+        __u8 copy                      : 1;
+        __u8 damage                    : 1;
+       __u8 reserved2                  : 2;
+       __u8 data_mode                  : 4;
+       __u8 fp                         : 1;
+       __u8 packet                     : 1;
+       __u8 blank                      : 1;
+       __u8 rt                         : 1;
+       __u8 nwa_v                      : 1;
+       __u8 lra_v                      : 1;
+       __u8 reserved3                  : 6;
+#endif
+       __u32 track_start;
+       __u32 next_writable;
+       __u32 free_blocks;
+       __u32 fixed_packet_size;
+       __u32 track_size;
+       __u32 last_rec_address;
+} track_information;
+
+extern int cdrom_get_disc_info(kdev_t dev, disc_information *di);
+extern int cdrom_get_track_info(kdev_t dev, __u16 track, __u8 type,
+                               track_information *ti);
+
+/* The SCSI spec says there could be 256 slots. */
+#define CDROM_MAX_SLOTS        256
+
+struct cdrom_mechstat_header {
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 fault         : 1;
+       __u8 changer_state : 2;
+       __u8 curslot       : 5;
+       __u8 mech_state    : 3;
+       __u8 door_open     : 1;
+       __u8 reserved1     : 4;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 curslot       : 5;
+       __u8 changer_state : 2;
+       __u8 fault         : 1;
+       __u8 reserved1     : 4;
+       __u8 door_open     : 1;
+       __u8 mech_state    : 3;
+#endif
+       __u8     curlba[3];
+       __u8     nslots;
+       __u16 slot_tablelen;
+};
+
+struct cdrom_slot {
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 disc_present : 1;
+       __u8 reserved1    : 6;
+       __u8 change       : 1;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 change       : 1;
+       __u8 reserved1    : 6;
+       __u8 disc_present : 1;
+#endif
+       __u8 reserved2[3];
+};
+
+struct cdrom_changer_info {
+       struct cdrom_mechstat_header hdr;
+       struct cdrom_slot slots[CDROM_MAX_SLOTS];
+};
+
+typedef enum {
+       mechtype_caddy = 0,
+       mechtype_tray  = 1,
+       mechtype_popup = 2,
+       mechtype_individual_changer = 4,
+       mechtype_cartridge_changer  = 5
+} mechtype_t;
+
+struct mode_page_header {
+       __u16 mode_data_length;
+       __u8 medium_type;
+       __u8 reserved1;
+       __u8 reserved2;
+       __u8 reserved3;
+       __u16 desc_length;
+};
+
+typedef struct {
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 ps                 : 1;
+       __u8 reserved1          : 1;
+       __u8 page_code          : 6;
+        __u8 page_length;
+       __u8 reserved2          : 1;
+       __u8 bufe               : 1;
+       __u8 ls_v               : 1;
+       __u8 test_write         : 1;
+        __u8 write_type                : 4;
+       __u8 multi_session      : 2; /* or border, DVD */
+       __u8 fp                 : 1;
+       __u8 copy               : 1;
+       __u8 track_mode         : 4;
+       __u8 reserved3          : 4;
+       __u8 data_block_type    : 4;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 page_code          : 6;
+       __u8 reserved1          : 1;
+       __u8 ps                 : 1;
+        __u8 page_length;
+        __u8 write_type                : 4;
+       __u8 test_write         : 1;
+       __u8 ls_v               : 1;
+       __u8 bufe               : 1;
+       __u8 reserved2          : 1;
+       __u8 track_mode         : 4;
+       __u8 copy               : 1;
+       __u8 fp                 : 1;
+       __u8 multi_session      : 2; /* or border, DVD */
+       __u8 data_block_type    : 4;
+       __u8 reserved3          : 4;
+#endif
+       __u8 link_size;
+       __u8 reserved4;
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 reserved5          : 2;
+       __u8 app_code           : 6;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 app_code           : 6;
+       __u8 reserved5          : 2;
+#endif
+       __u8 session_format;
+       __u8 reserved6;
+       __u32 packet_size;
+       __u16 audio_pause;
+       __u8 mcn[16];
+       __u8 isrc[16];
+       __u8 subhdr0;
+       __u8 subhdr1;
+       __u8 subhdr2;
+       __u8 subhdr3;
+} __attribute__((packed)) write_param_page;
+
+struct modesel_head
+{
+       __u8    reserved1;
+       __u8    medium;
+       __u8    reserved2;
+       __u8    block_desc_length;
+       __u8    density;
+       __u8    number_of_blocks_hi;
+       __u8    number_of_blocks_med;
+       __u8    number_of_blocks_lo;
+       __u8    reserved3;
+       __u8    block_length_hi;
+       __u8    block_length_med;
+       __u8    block_length_lo;
+};
+
+typedef struct {
+       __u16 report_key_length;
+       __u8 reserved1;
+       __u8 reserved2;
+#if defined(__BIG_ENDIAN_BITFIELD)
+       __u8 type_code                  : 2;
+       __u8 vra                        : 3;
+       __u8 ucca                       : 3;
+#elif defined(__LITTLE_ENDIAN_BITFIELD)
+       __u8 ucca                       : 3;
+       __u8 vra                        : 3;
+       __u8 type_code                  : 2;
+#endif
+       __u8 region_mask;
+       __u8 rpc_scheme;
+       __u8 reserved3;
+} rpc_state_t;
+
+#endif  /* End of kernel only stuff */ 
+
+#endif  /* _LINUX_CDROM_H */
index 00592074299704f531ee453b4f4d9db8d6a5dad0..383561043d430ccba6609f06a65fb318840c07fc 100644 (file)
@@ -287,7 +287,7 @@ CONFIG_JBD=y
 # CONFIG_CRAMFS is not set
 # CONFIG_TMPFS is not set
 # CONFIG_RAMFS is not set
-# CONFIG_ISO9660_FS is not set
+CONFIG_ISO9660_FS=y
 # CONFIG_MINIX_FS is not set
 # CONFIG_VXFS_FS is not set
 # CONFIG_NTFS_FS is not set